{"id":465,"date":"2013-01-29T09:12:08","date_gmt":"2013-01-29T07:12:08","guid":{"rendered":"https:\/\/polyetilen.lt\/?p=465"},"modified":"2023-04-18T10:07:22","modified_gmt":"2023-04-18T07:07:22","slug":"resize-and-crop-image-from-center-with-php","status":"publish","type":"post","link":"https:\/\/polyetilen.lt\/en\/resize-and-crop-image-from-center-with-php","title":{"rendered":"Resize and crop image from center with PHP"},"content":{"rendered":"<p>Sometimes I need to not only resize the image, but also to change its size by removing unnecessary edges for new size. This function changes image size and if the height or width is too large after resizing then the edges are cut off from the center. Input image formats are jpeg, gif, png.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/resize and crop image by center\r\nfunction resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){\r\n\t$imgsize = getimagesize($source_file);\r\n\t$width = $imgsize&#x5B;0];\r\n\t$height = $imgsize&#x5B;1];\r\n\t$mime = $imgsize&#x5B;'mime'];\r\n\r\n\tswitch($mime){\r\n\t\tcase 'image\/gif':\r\n\t\t\t$image_create = &quot;imagecreatefromgif&quot;;\r\n\t\t\t$image = &quot;imagegif&quot;;\r\n\t\t\tbreak;\r\n\r\n\t\tcase 'image\/png':\r\n\t\t\t$image_create = &quot;imagecreatefrompng&quot;;\r\n\t\t\t$image = &quot;imagepng&quot;;\r\n\t\t\t$quality = 7;\r\n\t\t\tbreak;\r\n\r\n\t\tcase 'image\/jpeg':\r\n\t\t\t$image_create = &quot;imagecreatefromjpeg&quot;;\r\n\t\t\t$image = &quot;imagejpeg&quot;;\r\n\t\t\t$quality = 80;\r\n\t\t\tbreak;\r\n\r\n\t\tdefault:\r\n\t\t\treturn false;\r\n\t\t\tbreak;\r\n\t}\r\n\t\r\n\t$dst_img = imagecreatetruecolor($max_width, $max_height);\r\n\t$src_img = $image_create($source_file);\r\n\t\r\n\t$width_new = $height * $max_width \/ $max_height;\r\n\t$height_new = $width * $max_height \/ $max_width;\r\n\t\/\/if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa\r\n\tif($width_new &gt; $width){\r\n\t\t\/\/cut point by height\r\n\t\t$h_point = (($height - $height_new) \/ 2);\r\n\t\t\/\/copy image\r\n\t\timagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);\r\n\t}else{\r\n\t\t\/\/cut point by width\r\n\t\t$w_point = (($width - $width_new) \/ 2);\r\n\t\timagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);\r\n\t}\r\n\t\r\n\t$image($dst_img, $dst_dir, $quality);\r\n\r\n\tif($dst_img)imagedestroy($dst_img);\r\n\tif($src_img)imagedestroy($src_img);\r\n}\r\n\/\/usage example\r\nresize_crop_image(100, 100, &quot;test.jpg&quot;, &quot;test.jpg&quot;);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes I need to not only resize the image, but also to change its size by removing unnecessary edges for new size. This function changes image size and if the height or width is too &hellip; <a href=\"https:\/\/polyetilen.lt\/en\/resize-and-crop-image-from-center-with-php\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"http:\/\/polyetilen.lt\/?p=316","footnotes":""},"categories":[8],"tags":[129,131,130,128],"class_list":["post-465","post","type-post","status-publish","format-standard","hentry","category-programavimas","tag-apkarpyti","tag-imagecopyresampled","tag-sumazinti","tag-vaizdas","en-US"],"_links":{"self":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/comments?post=465"}],"version-history":[{"count":1,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/465\/revisions"}],"predecessor-version":[{"id":466,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/465\/revisions\/466"}],"wp:attachment":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/media?parent=465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/categories?post=465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/tags?post=465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}