{"id":116,"date":"2016-10-23T23:27:30","date_gmt":"2016-10-23T14:27:30","guid":{"rendered":"http:\/\/www.dogrow.net\/php\/?p=116"},"modified":"2021-12-09T19:30:26","modified_gmt":"2021-12-09T10:30:26","slug":"blog9","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/php\/blog9\/","title":{"rendered":"(9) GD\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u753b\u50cf\u3092\u767d\u9ed2\u753b\u50cf\u306b\u5909\u63db"},"content":{"rendered":"<h1 class=my_h>\u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068<\/h1>\n<p>\u4efb\u610f\u306eJPEG\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092WEB\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3001\u767d\u9ed2\u753b\u50cf\u306b\u5909\u63db\u3057\u3066WEB\u30d6\u30e9\u30a6\u30b6\u306b\u8868\u793a\u3059\u308b\u3002<br \/>\n\u3053\u3053\u3067\u306f ImageMagick\u306a\u3069\u306e\u4fbf\u5229\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u308f\u305a\u3001\u611a\u76f4\u306b GD\u3092\u4f7f\u3063\u3066\u753b\u50cf\u3092\u7de8\u96c6\u3059\u308b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/10\/sample_c.jpg\" alt=\"sample_c\" width=\"280\" height=\"209\" class=\"alignnone size-full wp-image-122\" \/><br \/>\n\u3000\u2193<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/10\/sample_g.jpg\" alt=\"sample_g\" width=\"280\" height=\"209\" class=\"alignnone size-full wp-image-123\" \/><\/p>\n<h1 class=my_h>\u30d7\u30ed\u30b0\u30e9\u30e0\u4f5c\u6210<\/h1>\n<p>\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4ed5\u69d8\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<br \/>\n\u30fb\u5909\u63db\u53ef\u80fd\u306a\u753b\u50cf\u5f62\u5f0f\u306f JPEG, PNG\u306e2\u7a2e\u985e\u3068\u3059\u308b\u3002<br \/>\n\u30fb\u4e00\u8fba\u304c1000\u30d4\u30af\u30bb\u30eb\u4ee5\u4e0b\u306e\u753b\u50cf\u3092\u51e6\u7406\u5bfe\u8c61\u3068\u3059\u308b\u3002<\/p>\n<h3 class=my_h>index.php<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;HTML&gt;\r\n&lt;HEAD&gt;\r\n&lt;META CHARSET=&quot;UTF-8&quot; \/&gt;\r\n&lt;TITLE&gt;\u30b5\u30f3\u30d7\u30eb&lt;\/TITLE&gt;\r\n&lt;\/HEAD&gt;\r\n&lt;\/BODY&gt;\r\n&lt;?php\r\n  \/\/ \u753b\u50cf\u5909\u63db\u51e6\u7406\u8981\u6c42\uff1f\r\n  if(isset($_POST&#x5B;'exec'])){\r\n    try{\r\n      if(is_uploaded_file($_FILES&#x5B;'upfile']&#x5B;'tmp_name'])){\r\n        \/\/ \u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u305f\u753b\u50cf\u3092\u4fdd\u5b58\r\n        move_uploaded_file($_FILES&#x5B;'upfile']&#x5B;'tmp_name'], 'tmp.img');\r\n        \/\/ \u753b\u50cf\u5909\u63db\uff06\u8868\u793a\r\n        echo &quot;&lt;img src=&quot;img_change.php?fpath=tmp.img&quot;&gt;&quot;;\r\n      }\r\n    }catch(Exception $e){\r\n      echo &quot;&#x5B;&quot;.__LINE__.&quot;] Caught exception: &quot;.$e-&gt;getMessage();\r\n    }\r\n  }\r\n?&gt;\r\n&lt;hr \/&gt;\r\n&lt;form enctype=&quot;multipart\/form-data&quot; action=&quot;.\/&quot; method=&quot;post&quot;&gt;\r\n&lt;input type=&quot;file&quot; name=&quot;upfile&quot;&gt;&lt;br \/&gt;\r\n&lt;input name=&quot;exec&quot; type=&quot;submit&quot; value=&quot;JPEG,PNG\u753b\u50cf\u3092\u30b0\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\u753b\u50cf\u306b\u5909\u63db\u3057\u307e\u3059\u3002(\u4e00\u8fba1000\u30d4\u30af\u30bb\u30eb\u4ee5\u4e0b)&quot;&gt;\r\n&lt;\/form&gt;\r\n&lt;\/BODY&gt;\r\n&lt;\/HTML&gt;\r\n<\/pre>\n<h3 class=my_h>img_change.php<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nconvert_img($_GET&#x5B;'fpath']);\r\n\r\n\/\/ \u753b\u50cf\u5909\u63db\uff06\u51fa\u529b\r\nfunction convert_img( $fpath ){\r\n  \/\/ \u753b\u50cf\u60c5\u5831\u3092\u53d6\u5f97\r\n  $ary_info = getimagesize( $fpath );\r\n  if($ary_info === FALSE){\r\n    goto tagEND;\r\n  }\r\n  $image_type = $ary_info&#x5B;2];\r\n\r\n  \/\/ \u753b\u50cf\u51e6\u7406\u3092\u9078\u629e\r\n  $func_create  = NULL;\r\n  $func_display = NULL;\r\n  switch( $image_type ){\r\n  case IMAGETYPE_JPEG:\r\n    $func_create  = imagecreatefromjpeg;\r\n    $func_display = imagejpeg;\r\n    break;\r\n  case IMAGETYPE_PNG:\r\n    $func_create  = imagecreatefrompng;\r\n    $func_display = imagepng;\r\n    break;\r\n  default:\r\n    break;\r\n  }\r\n  if($func_create === NULL){\r\n    goto tagEND;\r\n  }\r\n\r\n  \/\/ \u753b\u50cf\u51fa\u529b\r\n  $mime_type = image_type_to_mime_type( $image_type );\r\n  header(&quot;Content-Type: &quot;.$mime_type);\r\n  $im = $func_create( $fpath );\r\n  \/\/ \u30b0\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\u753b\u50cf\u306b\u5909\u63db\r\n  conv_img_grayscale( $im, $im_out );\r\n  $func_display( $im_out );\r\n  imagedestroy( $im );\r\n  imagedestroy( $im_out );\r\ntagEND:\r\n}\r\n\r\n\/\/ \u5165\u529b\u753b\u50cf\u3092\u30b0\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\u753b\u50cf\u306b\u5909\u63db\r\nfunction conv_img_grayscale( $im_in, &amp;$im_out ){\r\n  $img_w = imageSX($im_in);\r\n  $img_h = imageSY($im_in);\r\n\r\n  for($y = 0 ; $y &lt; $img_h ; $y++){\r\n    for($x = 0 ; $x &lt;$img_w ; $x++){\r\n      \/\/ \u753b\u7d20\u5024\u3092\u53d6\u5f97\r\n      $cRGB = imagecolorat($im_in, $x, $y);\r\n      $cR = ($cRGB &gt;&gt; 16) &amp; 0xFF;\r\n      $cG = ($cRGB &gt;&gt; 8)  &amp; 0xFF;\r\n      $cB =  $cRGB &amp; 0xFF;\r\n      $g  = round(($cR + $cG + $cB) \/ 3);\r\n      \/\/ \u753b\u7d20\u5024\u3092\u4f5c\u6210\r\n      $gR = $g &lt;&lt; 16;\r\n      $gG = $g &lt;&lt; 8;\r\n      $gB = $g;\r\n      $gRGB = $gR | $gG | $gB;\r\n      imagesetpixel($im_in, $x, $y, $gRGB);\r\n      imagecolorallocate($im_in, $g, $g, $g);\r\n    }\r\n  }\r\n\r\n  \/\/ \u4f5c\u6210\u3057\u305f\u30d5\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\u753b\u50cf\u3092\u51fa\u529b\r\n  $im_out = ImageCreateTrueColor($img_w, $img_h);\r\n  imagecopy($im_out, $im_in, 0, 0, 0, 0, $img_w, $img_h);\r\n}\r\n?&gt;\r\n<\/pre>\n<p>\u3000<\/p>\n<hr class=my_hr_bottom>\n","protected":false},"excerpt":{"rendered":"<p>\u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068 \u4efb\u610f\u306eJPEG\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092WEB\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3001\u767d\u9ed2\u753b\u50cf\u306b\u5909\u63db\u3057\u3066WEB\u30d6\u30e9\u30a6\u30b6\u306b\u8868\u793a\u3059\u308b\u3002 \u3053\u3053\u3067\u306f ImageMagick\u306a\u3069\u306e\u4fbf\u5229\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u308f\u305a\u3001\u611a\u76f4\u306b GD\u3092\u4f7f\u3063\u3066\u753b\u50cf\u3092\u7de8\u96c6\u3059\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/php\/blog9\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,3,5,2],"tags":[],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-gd","category-http","category-5","category-2"],"views":5375,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":24,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":1988,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/116\/revisions\/1988"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}