{"id":261,"date":"2011-08-10T11:53:00","date_gmt":"2011-08-10T11:53:00","guid":{"rendered":"http:\/\/www.websitedesignwebsitedevelopment.com\/?p=261"},"modified":"2014-03-19T09:14:19","modified_gmt":"2014-03-19T09:14:19","slug":"resize-image-with-php-image-libraries-gd2-and-imagick","status":"publish","type":"post","link":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/","title":{"rendered":"Resize image with PHP image libraries gd2 and imagick"},"content":{"rendered":"<p>function smart_resize_image($file,<br \/>\n                              $width = 0,<br \/>\n                              $height = 0,<br \/>\n                              $proportional = false,<br \/>\n                              $output = &#8216;file&#8217;,<br \/>\n                              $delete_original = true,<br \/>\n                              $use_linux_commands = false,<br \/>\n\t\t\t\t\t\t\t  $imagick=FALSE ) {<\/p>\n<p>    if ( $height <= 0 &#038;&#038; $width <= 0 ) return false;\n\t\n    # Setting defaults and meta\n    $info = getimagesize($file);\n    $image = '';\n    $final_width = 0;\n    $final_height = 0;\n    list($width_old, $height_old) = $info;\t\n\t\n\t\n\tif($imagick)\n\t{\n\/*\t\t \t echo $file;\n\t\t\t echo '<br \/>&#8216;;<br \/>\n\t\t\t echo $output;*\/<\/p>\n<p>\t\t\t $im = new imagick($file);<br \/>\n\t\t\t \/* create the thumbnail *\/<br \/>\n\t\t\t $im->thumbnailImage($width,$height,$proportional);<br \/>\n\t\t\t \/* Write to a file *\/<br \/>\n\t\t\t $im->writeImage($output);<\/p>\n<p>\t}<br \/>\n\telse<br \/>\n\t{<\/p>\n<p>\t\/\/echo $output;<\/p>\n<p>    # Calculating proportionality<br \/>\n    if ($proportional) {<br \/>\n      if ($width == 0) $factor = $height\/$height_old;<br \/>\n      elseif ($height == 0) $factor = $width\/$width_old;<br \/>\n      else $factor = min( $width \/ $width_old, $height \/ $height_old );<\/p>\n<p>      $final_width = round( $width_old * $factor );<br \/>\n      $final_height = round( $height_old * $factor );<br \/>\n    }<br \/>\n    else {<br \/>\n      $final_width = ( $width <= 0 ) ? $width_old : $width;\n      $final_height = ( $height <= 0 ) ? $height_old : $height;\n    }\n\n\n    # Loading image to memory according to type\n    switch ( $info[2] ) {\n      case IMAGETYPE_GIF: $image = imagecreatefromgif($file); break;\n      case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($file); break;\n      case IMAGETYPE_PNG: $image = imagecreatefrompng($file); break;\n      default: return false;\n    }\n   \n   \n    # This is the resizing\/resampling\/transparency-preserving magic\n    $image_resized = imagecreatetruecolor( $final_width, $final_height );\n    if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {\n      $transparency = imagecolortransparent($image);\n\n\n      if ($transparency >= 0) {<br \/>\n        $transparent_color = @imagecolorsforindex($image, $trnprt_indx);<br \/>\n        $transparency = imagecolorallocate($image_resized, $trnprt_color[&#8216;red&#8217;], $trnprt_color[&#8216;green&#8217;], $trnprt_color[&#8216;blue&#8217;]);<br \/>\n        imagefill($image_resized, 0, 0, $transparency);<br \/>\n        imagecolortransparent($image_resized, $transparency);<br \/>\n      }<br \/>\n      elseif ($info[2] == IMAGETYPE_PNG) {<br \/>\n        imagealphablending($image_resized, false);<br \/>\n        $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);<br \/>\n        imagefill($image_resized, 0, 0, $color);<br \/>\n        imagesavealpha($image_resized, true);<br \/>\n      }<br \/>\n    }<br \/>\n    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);<\/p>\n<p>    # Taking care of original, if needed<br \/>\n    if ( $delete_original ) {<\/p>\n<p>      if ( $use_linux_commands ) exec(&#8216;rm &#8216;.$file);<br \/>\n      else @unlink($file);<br \/>\n    }<\/p>\n<p>    # Preparing a method of providing result<br \/>\n    switch ( strtolower($output) ) {<br \/>\n      case &#8216;browser&#8217;:<br \/>\n        $mime = image_type_to_mime_type($info[2]);<br \/>\n        header(&#8220;Content-type: $mime&#8221;);<br \/>\n        $output = NULL;<br \/>\n      break;<br \/>\n      case &#8216;file&#8217;:<br \/>\n        $output = $file;<br \/>\n      break;<br \/>\n      case &#8216;return&#8217;:<br \/>\n        return $image_resized;<br \/>\n      break;<br \/>\n      default:<br \/>\n      break;<br \/>\n    }<\/p>\n<p>    # Writing image according to type to the output destination<br \/>\n    switch ( $info[2] ) {<br \/>\n      case IMAGETYPE_GIF: imagegif($image_resized, $output); break;<br \/>\n      case IMAGETYPE_JPEG: imagejpeg($image_resized, $output); break;<br \/>\n      case IMAGETYPE_PNG: imagepng($image_resized, $output); break;<br \/>\n      default: return false;<br \/>\n    }<\/p>\n<p>\t}<br \/>\n    \/\/return true;<br \/>\n  } <\/p>\n","protected":false},"excerpt":{"rendered":"<p>function smart_resize_image($file, $width = 0, $height = 0, $proportional = false, $output = &#8216;file&#8217;, $delete_original = true, $use_linux_commands = false, $imagick=FALSE ) { if ( $height<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[25],"tags":[],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-image-manipulation"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Resize image with PHP image libraries gd2 and imagick<\/title>\n<meta name=\"description\" content=\"Resize image with PHP image libraries gd2 and imagick\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Resize image with PHP image libraries gd2 and imagick\" \/>\n<meta property=\"og:description\" content=\"Resize image with PHP image libraries gd2 and imagick\" \/>\n<meta property=\"og:url\" content=\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/\" \/>\n<meta property=\"og:site_name\" content=\"AndroidBubble Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/pages\/Website-Design-Website-Development\/172363336135453\" \/>\n<meta property=\"article:published_time\" content=\"2011-08-10T11:53:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-03-19T09:14:19+00:00\" \/>\n<meta name=\"author\" content=\"Fahad\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Fahad\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/\"},\"author\":{\"name\":\"Fahad\",\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7\"},\"headline\":\"Resize image with PHP image libraries gd2 and imagick\",\"datePublished\":\"2011-08-10T11:53:00+00:00\",\"dateModified\":\"2014-03-19T09:14:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/\"},\"wordCount\":86,\"articleSection\":[\"Image Manipulation\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/\",\"url\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/\",\"name\":\"Resize image with PHP image libraries gd2 and imagick\",\"isPartOf\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/#website\"},\"datePublished\":\"2011-08-10T11:53:00+00:00\",\"dateModified\":\"2014-03-19T09:14:19+00:00\",\"author\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7\"},\"description\":\"Resize image with PHP image libraries gd2 and imagick\",\"breadcrumb\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/androidbubble.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Website Development\",\"item\":\"https:\/\/androidbubble.com\/blog\/category\/website-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Image Manipulation\",\"item\":\"https:\/\/androidbubble.com\/blog\/category\/website-development\/image-manipulation\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Resize image with PHP image libraries gd2 and imagick\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/androidbubble.com\/blog\/#website\",\"url\":\"https:\/\/androidbubble.com\/blog\/\",\"name\":\"AndroidBubble Blog\",\"description\":\"Articles &amp; Posts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/androidbubble.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7\",\"name\":\"Fahad\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/afab5bcad71955d142a69df1758e6710aed4ab9cc2b5da28c54a7c3f39801b73?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/afab5bcad71955d142a69df1758e6710aed4ab9cc2b5da28c54a7c3f39801b73?s=96&d=mm&r=g\",\"caption\":\"Fahad\"},\"sameAs\":[\"http:\/\/www.androidbubbles.com\/\",\"https:\/\/x.com\/phpFlex\"],\"url\":\"https:\/\/androidbubble.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Resize image with PHP image libraries gd2 and imagick","description":"Resize image with PHP image libraries gd2 and imagick","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/","og_locale":"en_US","og_type":"article","og_title":"Resize image with PHP image libraries gd2 and imagick","og_description":"Resize image with PHP image libraries gd2 and imagick","og_url":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/","og_site_name":"AndroidBubble Blog","article_publisher":"https:\/\/www.facebook.com\/pages\/Website-Design-Website-Development\/172363336135453","article_published_time":"2011-08-10T11:53:00+00:00","article_modified_time":"2014-03-19T09:14:19+00:00","author":"Fahad","twitter_misc":{"Written by":"Fahad"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/#article","isPartOf":{"@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/"},"author":{"name":"Fahad","@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7"},"headline":"Resize image with PHP image libraries gd2 and imagick","datePublished":"2011-08-10T11:53:00+00:00","dateModified":"2014-03-19T09:14:19+00:00","mainEntityOfPage":{"@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/"},"wordCount":86,"articleSection":["Image Manipulation"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/","url":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/","name":"Resize image with PHP image libraries gd2 and imagick","isPartOf":{"@id":"https:\/\/androidbubble.com\/blog\/#website"},"datePublished":"2011-08-10T11:53:00+00:00","dateModified":"2014-03-19T09:14:19+00:00","author":{"@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7"},"description":"Resize image with PHP image libraries gd2 and imagick","breadcrumb":{"@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/resize-image-with-php-image-libraries-gd2-and-imagick\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/androidbubble.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Website Development","item":"https:\/\/androidbubble.com\/blog\/category\/website-development\/"},{"@type":"ListItem","position":3,"name":"Image Manipulation","item":"https:\/\/androidbubble.com\/blog\/category\/website-development\/image-manipulation\/"},{"@type":"ListItem","position":4,"name":"Resize image with PHP image libraries gd2 and imagick"}]},{"@type":"WebSite","@id":"https:\/\/androidbubble.com\/blog\/#website","url":"https:\/\/androidbubble.com\/blog\/","name":"AndroidBubble Blog","description":"Articles &amp; Posts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/androidbubble.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7","name":"Fahad","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/afab5bcad71955d142a69df1758e6710aed4ab9cc2b5da28c54a7c3f39801b73?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/afab5bcad71955d142a69df1758e6710aed4ab9cc2b5da28c54a7c3f39801b73?s=96&d=mm&r=g","caption":"Fahad"},"sameAs":["http:\/\/www.androidbubbles.com\/","https:\/\/x.com\/phpFlex"],"url":"https:\/\/androidbubble.com\/blog\/author\/admin\/"}]}},"views":618,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/comments?post=261"}],"version-history":[{"count":1,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts\/261\/revisions\/262"}],"wp:attachment":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/media?parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/categories?post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/tags?post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}