I am making an online store so, I need to merge the design image with the mockup image and because of having a lot of mockups the merging process tooks too long time about 5min for each design. I'm looking for a quicker function from the one that I am using. this my php function:
function createimageinstantly($img1, $img2, $color, $randF){
// check the file format
if(end(explode(".", $img2)) == "png"){$source = imagecreatefrompng($img2);}else{$source = imagecreatefromjpeg($img2);}
if(end(explode(".", $img1)) == "png"){$destination = imagecreatefrompng($img1);}else{$destination = imagecreatefromjpeg($img1);}
//to resize the second image start
list($width, $height) = getimagesize($img2);
list($width1, $height1) = getimagesize($img1);
$percent = ((intval($width1 * 100 / $width) * 0.34) / 100);
// Calculate new sizes
$newwidth = $width * $percent;
$newheight = $height * $percent;
$chupon = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($chupon, false);
imagesavealpha($chupon,true);
$transparent = imagecolorallocatealpha($chupon, 255, 255, 255, 127);
imagefilledrectangle($chupon, 0, 0, $newwidth, $newheight, $transparent);
imagecopyresized($chupon, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// merge images
imagecopy($destination, $chupon, (imagesx($destination) / 2) - (imagesx($chupon) / 2), (imagesy($destination) / 2) - (imagesy($chupon) / 2 + imagesy($chupon) / 4), 0, 0, imagesx($chupon), imagesy($chupon));
//header("Content-type: image/png");
$file_name = end(explode("/", $img2));
$file_name = explode(".", $file_name);
//Output the image
if(end(explode(".", $img1)) == "png"){imagepng($destination, "../uploades/show/".$file_name[0].$randF."_".$color.".".$file_name[1]);}else{imagejpeg($destination, "../uploades/show/".$file_name[0].$randF."_".$color.".".$file_name[1]);}
}
It tooks the design image in $img1 and the mockup image in $img2, the $color and $randF are just a variables to change the name of the output image. In this process I merge the design image after resizing it to fit on top of the mockup image in order to produce something like:
I hope you can help me ! These are an example for the images that I am using: the mockup:
I have made some simplyfications. For example i use the mockup as background and scale the design itself instead of coping it to the smaller empty image.
Important: the output needs to be
.png
because ony so you get a transperent background. Also use.png
for better transparent results for the overlaing
<?php
function createimageinstantly($design, $mockup){
// check the file format
if(end(explode(".", $design)) == "png"){$designi = imagecreatefrompng($design);}else{$designi = imagecreatefromjpeg($design);}
if(end(explode(".", $mockup)) == "png"){$mockupi = imagecreatefrompng($mockup);}else{$mockupi = imagecreatefromjpeg($mockup);}
//to resize the second image start
list($width1, $height1) = getimagesize($mockup);
imagealphablending($designi, false);
imagesavealpha($designi, true);
imagecolortransparent($designi, imagecolorallocate($designi, 255, 255, 255));
$designi = imagescale($designi, $width1 * 0.34);
$newwidth = imagesx($designi);
$newheight = imagesy($designi);
// imagealphablending($mockupi, false);
imagesavealpha($mockupi, true);
imagefill($mockupi, 0, 0, imagecolorallocatealpha($mockupi, 0, 0, 0, 127));
// merge images
imagecopy($mockupi, $designi, ($width1 - $newwidth) / 2, ($height1 - $newheight * 3/2) / 2, 0, 0, $newwidth, $newheight);
// $file_name = end(explode("/", $design));
// $file_name = explode(".", $file_name);
//Output the image
imagepng($mockupi, __DIR__."/test.png");
}
createimageinstantly(
__DIR__."/design-png-hd-graphic-design-transparent-1590.png",
__DIR__."/otHl3.png"
);
?>
Info: for me the code only took 1 second to execute and the original did the same. At some point you can't strip down the code enougth to shorten execution time.