{"id":253,"date":"2011-08-10T11:48:42","date_gmt":"2011-08-10T11:48:42","guid":{"rendered":"http:\/\/www.websitedesignwebsitedevelopment.com\/?p=253"},"modified":"2014-03-19T09:14:19","modified_gmt":"2014-03-19T09:14:19","slug":"merge-two-images-with-php-image-libraries-gd2-and-imagick","status":"publish","type":"post","link":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/","title":{"rendered":"Merge two images with php image libraries gd2 and imagick"},"content":{"rendered":"<p>\tfunction mergeFrontBack($img_front, $img_back, $final_output, $x, $y, $imagick=FALSE)<br \/>\n\t{<\/p>\n<p>\t\tif($imagick)<br \/>\n\t\t{<\/p>\n<p>\t\t\t$overlay = new Imagick($img_front);<br \/>\n\t\t\t$bg = new Imagick($img_back);<br \/>\n\t\t\t$temp_image = new Imagick();<\/p>\n<p>\t\t\t$d = $overlay->getImageGeometry();<br \/>\n\t\t\t$w = $d[&#8216;width&#8217;];<br \/>\n\t\t\t$h = $d[&#8216;height&#8217;]; \t\t<\/p>\n<p>\t\t\t$bg_temp = $this->prefixToImg(&#8216;_temp&#8217;,$bg);<br \/>\n\t\t\t$bg_copy = $this->prefixToImg(&#8216;_copy&#8217;,$bg);<\/p>\n<p>\t\t\t$temp_image->newImage($w, $h, new ImagickPixel(&#8216;white&#8217;));<br \/>\n\t\t\t$temp_image->setImageFormat(&#8216;png&#8217;);<br \/>\n\t\t\t$temp_image->writeImage($bg_temp);<\/p>\n<p>\t\t\t\/\/ Set the colorspace to the same value<br \/>\n\t\t\t$temp_image->setImageColorspace($bg->getImageColorspace()); <\/p>\n<p>\t\t\t\/\/Second image is put on top of the first<br \/>\n\t\t\t$temp_image->compositeImage($bg, $bg->getImageCompose(), $x, $y); <\/p>\n<p>\t\t\t$temp_image->writeImage($bg_copy);<\/p>\n<p>\t\t\t$final_copy = new Imagick($bg_copy); <\/p>\n<p>\t\t\t\/\/ Set the colorspace to the same value<br \/>\n\t\t\t$final_copy->setImageColorspace($overlay->getImageColorspace() ); <\/p>\n<p>\t\t\t\/\/Second image is put on top of the first<br \/>\n\t\t\t$final_copy->compositeImage($overlay, $overlay->getImageCompose(), 0, 0); <\/p>\n<p>\t\t\t\/\/new image is saved as final.jpg<br \/>\n\t\t\t$final_copy->writeImage($final_output); \t<\/p>\n<p>\t\t\tif(file_exists($bg_temp))<br \/>\n\t\t\tunlink($bg_temp);<\/p>\n<p>\t\t\tif(file_exists($bg_copy))<br \/>\n\t\t\tunlink($bg_copy);<\/p>\n<p>\t\t}<br \/>\n\t\telse<br \/>\n\t\t{<\/p>\n<p>\t\t$img_front = $this->getImgSrc($img_front);<br \/>\n\t\t$img_back = $this->getImgSrc($img_back);<\/p>\n<p>\t\t$width = imagesx($img_front);<br \/>\n\t\t$height = imagesy($img_front);<\/p>\n<p>\t\t$swidth = imagesx($img_back);<br \/>\n\t\t$sheight = imagesy($img_back);\t<\/p>\n<p>\t\t$im = imagecreatetruecolor($width, $height);<br \/>\n\t\t\/\/$white = imagecolorallocate($im, 255, 255, 255);<br \/>\n\t\t$black = imagecolorallocate($im, 0, 0, 0);<\/p>\n<p>\t\t\/\/ Make the background transparent<br \/>\n\t\timagecolortransparent($im, $black);<\/p>\n<p>\t\t\/\/imagepng($im, RELA.&#8217;test1.png&#8217;);<\/p>\n<p>\t\timagecopy ($im, $img_back , $x , $y , 0 , 0 , $width , $height );<\/p>\n<p>\t\t\/\/imagecopyresampled($im, $img_back , $x , $y , 0 , 0 , $width , $height, $swidth, $sheight  );<\/p>\n<p>\t\t\/\/TRANSPARENT IMAGE CREATED SUCCESSFULLY<br \/>\n\t\t\/\/imagepng($im, RELA.&#8217;test2.png&#8217;);<\/p>\n<p>\t\timagecopy ($im, $img_front , 0 , 0 , 0 , 0 , $width , $height );<\/p>\n<p>\t\timagepng($im,$final_output,0);\t\t<\/p>\n<p>\t\timagedestroy($im);<\/p>\n<p>\t\t}<br \/>\n\t}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>function mergeFrontBack($img_front, $img_back, $final_output, $x, $y, $imagick=FALSE) { if($imagick) { $overlay = new Imagick($img_front); $bg = new Imagick($img_back); $temp_image = new Imagick(); $d = $overlay->getImageGeometry(); $w = $d[&#8216;width&#8217;]; $h = $d[&#8216;height&#8217;]; $bg_temp = $this->prefixToImg(&#8216;_temp&#8217;,$bg); $bg_copy = $this->prefixToImg(&#8216;_copy&#8217;,$bg); $temp_image->newImage($w, $h, new ImagickPixel(&#8216;white&#8217;)); $temp_image->setImageFormat(&#8216;png&#8217;); $temp_image->writeImage($bg_temp); \/\/ Set the colorspace to the same value $temp_image->setImageColorspace($bg->getImageColorspace()); \/\/Second image is [&hellip;]<\/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-253","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>Merge two images with php image libraries gd2 and imagick<\/title>\n<meta name=\"description\" content=\"Merge two images 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\/merge-two-images-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=\"Merge two images with php image libraries gd2 and imagick\" \/>\n<meta property=\"og:description\" content=\"Merge two images with php image libraries gd2 and imagick\" \/>\n<meta property=\"og:url\" content=\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-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:48:42+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\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\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\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/\"},\"author\":{\"name\":\"Fahad\",\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7\"},\"headline\":\"Merge two images with php image libraries gd2 and imagick\",\"datePublished\":\"2011-08-10T11:48:42+00:00\",\"dateModified\":\"2014-03-19T09:14:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/\"},\"wordCount\":261,\"articleSection\":[\"Image Manipulation\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/\",\"url\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/\",\"name\":\"Merge two images with php image libraries gd2 and imagick\",\"isPartOf\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/#website\"},\"datePublished\":\"2011-08-10T11:48:42+00:00\",\"dateModified\":\"2014-03-19T09:14:19+00:00\",\"author\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7\"},\"description\":\"Merge two images with php image libraries gd2 and imagick\",\"breadcrumb\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-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\":\"Merge two images 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":"Merge two images with php image libraries gd2 and imagick","description":"Merge two images 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\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/","og_locale":"en_US","og_type":"article","og_title":"Merge two images with php image libraries gd2 and imagick","og_description":"Merge two images with php image libraries gd2 and imagick","og_url":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-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:48:42+00:00","article_modified_time":"2014-03-19T09:14:19+00:00","author":"Fahad","twitter_misc":{"Written by":"Fahad","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/#article","isPartOf":{"@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/"},"author":{"name":"Fahad","@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7"},"headline":"Merge two images with php image libraries gd2 and imagick","datePublished":"2011-08-10T11:48:42+00:00","dateModified":"2014-03-19T09:14:19+00:00","mainEntityOfPage":{"@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/"},"wordCount":261,"articleSection":["Image Manipulation"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/","url":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/","name":"Merge two images with php image libraries gd2 and imagick","isPartOf":{"@id":"https:\/\/androidbubble.com\/blog\/#website"},"datePublished":"2011-08-10T11:48:42+00:00","dateModified":"2014-03-19T09:14:19+00:00","author":{"@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7"},"description":"Merge two images with php image libraries gd2 and imagick","breadcrumb":{"@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-with-php-image-libraries-gd2-and-imagick\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/androidbubble.com\/blog\/website-development\/image-manipulation\/merge-two-images-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":"Merge two images 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":1298,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts\/253","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=253"}],"version-history":[{"count":2,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":1709,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts\/253\/revisions\/1709"}],"wp:attachment":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/media?parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/categories?post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/tags?post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}