{"id":132,"date":"2016-10-24T03:38:18","date_gmt":"2016-10-23T18:38:18","guid":{"rendered":"http:\/\/www.dogrow.net\/php\/?p=132"},"modified":"2017-09-01T17:14:42","modified_gmt":"2017-09-01T08:14:42","slug":"blog11","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/php\/blog11\/","title":{"rendered":"(11) GD\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u753b\u50cf\u3092\u5207\u308a\u629c\u304d"},"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\u6307\u5b9a\u9818\u57df\u3092\u5207\u308a\u629c\u3044\u3066WEB\u30d6\u30e9\u30a6\u30b6\u306b\u8868\u793a\u3059\u308b\u3002<br \/>\nImageMagick\u3067\u306f\u306a\u304f GD\u3092\u4f7f\u7528\u3059\u308b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/10\/sample_org-1.jpg\" alt=\"sample_org\" width=\"400\" height=\"278\" class=\"alignnone size-full wp-image-147\" srcset=\"https:\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/10\/sample_org-1.jpg 400w, https:\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/10\/sample_org-1-300x209.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><br \/>\n\u3000\u2193<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/\/www.dogrow.net\/php\/wp-content\/uploads\/2016\/10\/sample_crop.jpg\" alt=\"sample_crop\" width=\"250\" height=\"100\" class=\"alignnone size-full wp-image-148\" \/><br \/>\n\u3000<br \/>\n\u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002<br \/>\n<a href=\"\/\/www.dogrow.net\/php\/sample\/00011\/\" target=\"_blank\">\/\/www.dogrow.net\/php\/sample\/00011\/<\/a><\/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, GIF\u306e3\u7a2e\u985e\u3068\u3059\u308b\u3002<br \/>\n\u30fb\u5165\u529b\u753b\u50cf\u306f\u4e00\u8fba\u304c20\uff5e1000\u30d4\u30af\u30bb\u30eb\u306e\u753b\u50cf\u3092\u51e6\u7406\u5bfe\u8c61\u3068\u3059\u308b\u3002<br \/>\n\u30fb\u51fa\u529b\u753b\u50cf\u30b5\u30a4\u30ba\u306f\u5165\u529b\u753b\u50cf\u30b5\u30a4\u30ba\u3088\u308a\u3082\u5c0f\u3055\u3044\u3082\u306e\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\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_crop.php?fpath=tmp.img&amp;x=&quot;.$_POST&#x5B;'x'].&quot;&amp;y=&quot;.$_POST&#x5B;'y'].&quot;&amp;w=&quot;.$_POST&#x5B;'w'].&quot;&amp;h=&quot;.$_POST&#x5B;'h'].&quot;\\&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\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;table style=&quot;border:2px #000 solid&quot;&gt;\r\n  &lt;tr&gt;&lt;th&gt;\u753b\u50cf\u30d5\u30a1\u30a4\u30eb&lt;\/th&gt;&lt;td&gt;&lt;input type=file name=upfile&gt;&lt;\/td&gt;&lt;\/tr&gt;\r\n  &lt;tr&gt;&lt;th&gt;\u59cb\u70b9X &#x5B;pixel]&lt;\/th&gt;&lt;td&gt;&lt;input type=text name=x value=&quot;0&quot;&gt;&lt;\/td&gt;&lt;\/tr&gt;\r\n  &lt;tr&gt;&lt;th&gt;\u59cb\u70b9Y &#x5B;pixel]&lt;\/th&gt;&lt;td&gt;&lt;input type=text name=y value=&quot;0&quot;&gt;&lt;\/td&gt;&lt;\/tr&gt;\r\n  &lt;tr&gt;&lt;th&gt;\u5e45    &#x5B;pixel]&lt;\/th&gt;&lt;td&gt;&lt;input type=text name=w value=&quot;100&quot;&gt;&lt;\/td&gt;&lt;\/tr&gt;\r\n  &lt;tr&gt;&lt;th&gt;\u9ad8\u3055  &#x5B;pixel]&lt;\/th&gt;&lt;td&gt;&lt;input type=text name=h value=&quot;100&quot;&gt;&lt;\/td&gt;&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n&lt;input name=&quot;exec&quot; type=&quot;submit&quot; value=&quot;JPEG,PNG,GIF\u753b\u50cf\u306e\u4e00\u90e8\u5206\u3092\u5207\u308a\u629c\u304d\u307e\u3059\u3002&quot;&gt;\r\n&lt;p style=&quot;border:2px #f00 solid;color:#f00;padding:2px;&quot;&gt;\r\n\u30fb\u5909\u63db\u53ef\u80fd\u306a\u753b\u50cf\u5f62\u5f0f\u306f JPEG, PNG, GIF\u306e3\u7a2e\u985e\u3068\u3059\u308b\u3002&lt;br \/&gt;\r\n\u30fb\u5165\u529b\u753b\u50cf\u306f\u4e00\u8fba\u304c20\uff5e1000\u30d4\u30af\u30bb\u30eb\u306e\u753b\u50cf\u3092\u51e6\u7406\u5bfe\u8c61\u3068\u3059\u308b\u3002&lt;br \/&gt;\r\n\u30fb\u51fa\u529b\u753b\u50cf\u30b5\u30a4\u30ba\u306f\u5165\u529b\u753b\u50cf\u30b5\u30a4\u30ba\u3088\u308a\u3082\u5c0f\u3055\u3044\u3082\u306e\u3068\u3059\u308b\u3002\r\n&lt;\/p&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt;\/BODY&gt;\r\n&lt;\/HTML&gt;\r\n<\/pre>\n<h3 class=my_h>img_crop.php<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\ncrop_img($_GET&#x5B;'fpath'], $_GET&#x5B;'x'], $_GET&#x5B;'y'], $_GET&#x5B;'w'], $_GET&#x5B;'h']);\r\n\r\n\/\/ \u753b\u50cf\u5909\u63db\uff06\u51fa\u529b\r\nfunction crop_img( $fpath, $x, $y, $w, $h ){\r\n\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  $img_w    = $ary_info&#x5B;0];\r\n  $img_h    = $ary_info&#x5B;1];\r\n  $img_type = $ary_info&#x5B;2];\r\n\r\n  \/\/ \u753b\u50cf\u30b5\u30a4\u30ba\u30c1\u30a7\u30c3\u30af\r\n  if(($img_w &gt; 1000) || ($img_h &gt; 1000)){\r\n    goto tagEND;\r\n  }\r\n  if(($img_w &lt; 20) || ($img_h &lt; 20)){\r\n    goto tagEND;\r\n  }\r\n\r\n  \/\/ \u5207\u308a\u629c\u304d\u59cb\u70b9\u7bc4\u56f2\u30c1\u30a7\u30c3\u30af\r\n  $x = max(0,        $x);\r\n  $x = min($img_w-1, $x);\r\n  $y = max(0,        $y);\r\n  $y = min($img_h-1, $y);\r\n  \/\/ \u5207\u308a\u629c\u304d\u30b5\u30a4\u30ba\u7bc4\u56f2\u30c1\u30a7\u30c3\u30af\r\n  $w = max(1, $w);\r\n  $w = min($img_w - $x, $w);\r\n  $h = max(1,           $h);\r\n  $h = min($img_h - $y, $h);\r\n  \/\/ \u77e9\u5f62\u6307\u5b9a\r\n  $rect = array();\r\n  $rect&#x5B;'x'] = $x;\r\n  $rect&#x5B;'y'] = $y;\r\n  $rect&#x5B;'width'] = $w;\r\n  $rect&#x5B;'height'] = $h;\r\n\r\n  \/\/ \u753b\u50cf\u51e6\u7406\u3092\u9078\u629e\r\n  $func_create  = NULL;\r\n  $func_display = NULL;\r\n  switch( $img_type ){\r\n  case IMAGETYPE_JPEG:\r\n    $func_create  = &quot;imagecreatefromjpeg&quot;;\r\n    $func_display = imagejpeg;\r\n    break;\r\n  case IMAGETYPE_PNG:\r\n    $func_create  = &quot;imagecreatefrompng&quot;;\r\n    $func_display = imagepng;\r\n    break;\r\n  case IMAGETYPE_GIF:\r\n    $func_create  = &quot;imagecreatefromgif&quot;;\r\n    $func_display = imagegif;\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\u3092\u5207\u308a\u629c\u304d\r\n  $im_in  = $func_create( $fpath );\r\n  $im_out = imagecrop($im_in, $rect);\r\n\r\n  \/\/ \u753b\u50cf\u51fa\u529b\r\n  $mime_type = image_type_to_mime_type( $img_type );\r\n  header(&quot;Content-Type: &quot;.$mime_type);\r\n  $func_display( $im_out );\r\n  imagedestroy( $im_in );\r\n  imagedestroy( $im_out );\r\ntagEND:\r\n}\r\n?&gt;\r\n<\/pre>\n<p>\u3000<br \/>\n\u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002<br \/>\n<a href=\"\/\/www.dogrow.net\/php\/sample\/00011\/\" target=\"_blank\">\/\/www.dogrow.net\/php\/sample\/00011\/<\/a><\/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\u6307\u5b9a\u9818\u57df\u3092\u5207\u308a\u629c\u3044\u3066WEB\u30d6\u30e9\u30a6\u30b6\u306b\u8868\u793a\u3059\u308b\u3002 ImageMagick\u3067\u306f\u306a\u304f GD\u3092\u4f7f\u7528\u3059\u308b\u3002 \u3000\u2193 \u3000 \u5b9f\u884c\u30b5\u30f3\u30d7\u30eb\u306f\u3053\u3061\u3089\u3067\u3059\u3002 \/\/\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/php\/blog11\/\">\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,5,2],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-gd","category-5","category-2"],"views":4592,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/132","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=132"}],"version-history":[{"count":15,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":788,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/posts\/132\/revisions\/788"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/php\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}