John6854654654 Posted June 15, 2009 Share Posted June 15, 2009 I want to do the following: -Upload image -Resize with max width and max height -Put resized uploaded image on top of predefined background image -Store in folder on serve I can do all of this, except i am having a problem with the imagecopymerge() function. It requires that i input a PCT which i have figured out basically means opacity, but a bit different. Anyways, when i put PCT to 100, the uploaded image shows up with a green background the size of the predefined background image. If i set PCT to 0, only the background image shows up. If i set the PCT to 50, i see a mess of the two images combined/faded together. How can i fix my code to have the uploaded image be on top of the background image? Here is my code: $file_realname = trim($_FILES['imgfile']['name']); // This is the temporary file created by PHP $uploadedfile = $_FILES['imgfile']['tmp_name']; $uploadedfile_type = $_FILES['imgfile']['type']; // Create an Image from it so we can do the resize if($uploadedfile_type == "image/gif"){$src = imagecreatefromgif($uploadedfile);} else if ($uploadedfile_type == "image/jpg"){$src = imagecreatefromjpeg($uploadedfile);} else if ($uploadedfile_type == "image/jpeg"){$src = imagecreatefromjpeg($uploadedfile);} else if ($uploadedfile_type == "image/png"){$src = imagecreatefrompng($uploadedfile);} else{ //Not JPG, JPEG, GIF, or PNG! header("Location: ./../../index.php?section=2&flyerid=$flyerid&fname=$fname&logo=invalid&preview=false"); } // Capture the original size of the uploaded image list($width,$height)=getimagesize($uploadedfile); $max_height = 63; $max_width = 71; if($width >= $height){ $newwidth = $max_width; $newheight = $height * ($max_width / $width); } else{ $newwidth = $width * ($max_height / $height); $newheight = $max_height; } $tmp=imagecreatetruecolor($newwidth,$newheight); // this line actually does the image resizing, copying from the original // image into the image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); //Merge Background $dest = imagecreatefromgif("images/logo.gif"); imagecopymerge($dest, $tmp, 0, 0, 0, 0, $max_width, $max_height, 0); // now write the resized image to disk. I have assumed that you want the // resized, uploaded image file to reside in the ./images subdirectory. $filename = "pages/logos/". $_FILES['imgfile']['name']; if($uploadedfile_type == "image/gif"){imagegif($dest,$filename,100);} else if ($uploadedfile_type == "image/jpg"){imagejpeg($dest,$filename,100);} else if ($uploadedfile_type == "image/jpeg"){imagejpeg($dest,$filename,100);} else if ($uploadedfile_type == "image/png"){imagepng($dest,$filename,100);} imagedestroy($dest); imagedestroy($src); imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request has completed. Link to comment https://forums.phpfreaks.com/topic/162287-solved-uploadingmanipulating-an-image/ Share on other sites More sharing options...
John6854654654 Posted June 16, 2009 Author Share Posted June 16, 2009 Thanks for all the help! Kidding, it was a lot to take on, i understand. But i made the fix, so here it is for anyone who wants it: $file_realname = trim($_FILES['imgfile']['name']); // This is the temporary file created by PHP $uploadedfile = $_FILES['imgfile']['tmp_name']; $uploadedfile_type = $_FILES['imgfile']['type']; // Create an Image from it so we can do the resize if($uploadedfile_type == "image/gif"){$src = imagecreatefromgif($uploadedfile);} else if ($uploadedfile_type == "image/jpg"){$src = imagecreatefromjpeg($uploadedfile);} else if ($uploadedfile_type == "image/jpeg"){$src = imagecreatefromjpeg($uploadedfile);} else if ($uploadedfile_type == "image/png"){$src = imagecreatefrompng($uploadedfile);} else{ //Not JPG, JPEG, GIF, or PNG! header("Location: ./../../index.php?section=2&flyerid=$flyerid&fname=$fname&logo=invalid&preview=false"); } // Capture the original size of the uploaded image list($width,$height)=getimagesize($uploadedfile); $max_height = 63; $max_width = 71; if($width >= $height){ $newwidth = $max_width; $newheight = $height * ($max_width / $width); } else{ $newwidth = $width * ($max_height / $height); $newheight = $max_height; } $tmp=imagecreatetruecolor($max_width, $max_height); // this line actually does the image resizing, copying from the original // image into the image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); //Merge Background $dest = imagecreatefromgif("images/logo.gif"); imagecopy($tmp, $dest, 0, $newheight, 0, $newheight, $max_width, $max_height); //imagecopymerge($dest, $tmp, 0, 0, 0, 0, $max_width, $max_height, 100); // now write the resized image to disk. I have assumed that you want the // resized, uploaded image file to reside in the ./images subdirectory. $filename = "pages/logos/". $_FILES['imgfile']['name']; if($uploadedfile_type == "image/gif"){imagegif($tmp,$filename,100);} else if ($uploadedfile_type == "image/jpg"){imagejpeg($tmp,$filename,100);} else if ($uploadedfile_type == "image/jpeg"){imagejpeg($tmp,$filename,100);} else if ($uploadedfile_type == "image/png"){imagepng($tmp,$filename,100);} imagedestroy($dest); imagedestroy($src); imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request has completed. Basically, it takes a user image from a form, resizes it to have a max size while still keeping proportions, takes a predefined background with the max size and puts it behind the user image. So now the image is the same size as the max proportions but with the users image vertically aligned at the top of my predefined background. Why didn't i just make the background image the style="background-img:url()"? You cant if your converting it to a PDF with TCPDF. So, this fixes that incase anyone wants that. Link to comment https://forums.phpfreaks.com/topic/162287-solved-uploadingmanipulating-an-image/#findComment-856776 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.