joma Posted December 14, 2007 Share Posted December 14, 2007 I am running oscommerce and need to be able to have staff add products to the page, problem is they are all too stupid to do things how i want them to, so i want to keep the page as dynamic as i can, i am using this to resize images if they are too large for the page, to save bandwidth and such. //start $filename = $this->destination . $this->filename; $maxHeight = 498; $maxWidth = 664; $extension = substr($filename,-3); if($extension == 'gif') { $src = imagecreatefromgif($filename); } elseif($extension == 'png') { $src = imagecreatefrompng($filename); } elseif($extension == 'jpg'||$extension == 'jpeg') { $src = imagecreatefromjpeg($filename); } $oldWidth = imagesx($src); $oldHeight = imagesy($src); if($oldWidth > $maxWidth||$oldHeight > $maxHeight) { if(($maxWidth/$oldWidth) >= ($maxHeight/$oldHeight)) $factor = $maxHeight/$oldHeight; else $factor = $maxWidth/$oldWidth; $newWidth = $oldWidth*$factor; $newHeight = $oldHeight*$factor; } else { $newWidth = $oldWidth; $newHeight = $oldHeight; } $tmp = imagecreatetruecolor($newWidth,$newHeight); imagecopyresized($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight); imagejpeg($tmp,$filename,80); imagedestroy($tmp); imagedestroy($src); //end i have very basic knowledge in php and none with imagemagick. I want this script to also output a png watermark on the picture it is processing. The other thing is i have to make sure the image variables (output name and location ect..) stay intact to this script (because it is working) so that the rest of the script will continue to execute correctly. cheers/ Josh Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted December 15, 2007 Share Posted December 15, 2007 Topic covered.... http://www.phpfreaks.com/forums/index.php/topic,170674.0.html addapt varaibles Don't forget because of the fudge factor which makes the memory used within memory limit will be alot more than ur image... so make it ini_set("memory_limit","40M"); and you won't get the server is exahusted. Id recomend breaking the script up into two seperate files. 1 for resize image and save it 2 resend paths of files new and old to second file 3 new file watermarks image from file and outputs it Quote Link to comment Share on other sites More sharing options...
joma Posted December 15, 2007 Author Share Posted December 15, 2007 i am following what your saying but it is not registering to me still i pass this through an upload script on the page, are you saying the script should look like this //start resize $filename = $this->destination . $this->filename; $maxHeight = 498; $maxWidth = 664; $extension = substr($filename,-3); if($extension == 'gif') { $src = imagecreatefromgif($filename); } elseif($extension == 'png') { $src = imagecreatefrompng($filename); } elseif($extension == 'jpg'||$extension == 'jpeg') { $src = imagecreatefromjpeg($filename); } $oldWidth = imagesx($src); $oldHeight = imagesy($src); if($oldWidth > $maxWidth||$oldHeight > $maxHeight) { if(($maxWidth/$oldWidth) >= ($maxHeight/$oldHeight)) $factor = $maxHeight/$oldHeight; else $factor = $maxWidth/$oldWidth; $newWidth = $oldWidth*$factor; $newHeight = $oldHeight*$factor; } else { $newWidth = $oldWidth; $newHeight = $oldHeight; } $tmp = imagecreatetruecolor($newWidth,$newHeight); imagecopyresized($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight); imagejpeg($tmp,$filename,80); imagedestroy($tmp); imagedestroy($src); //end resize //start watermark $watermark = ImageCreateFromGif('watermark.gif'); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $image = imagecreatetruecolor($watermark_width, $watermark_height); //create image if (strstr($file_allocation_new,'jpg') || strstr($file_allocation_new,'JPG') || strstr($file_allocation_new,'jpeg') || strstr($file_allocation_new,'JPEG')) { $file_type="jpg"; } if (strstr($file_allocation_new,'png') || strstr($file_allocation_new,'PNG')) { $file_type="png"; } if (strstr($file_allocation_new,'gif') || strstr($file_allocation_new,'GIF')) { $file_type="gif"; } switch ($file_type) { case "jpg": $image = imagecreatefromjpeg($file_allocation_new); break; case "gif": $image = imagecreatefromGif($file_allocation_new); break; case "png": $image = imagecreatefromPng($file_allocation_new); break; } $size = getimagesize($file_allocation_new); $dest_x = $size[0] - $watermark_width - 5; $dest_y = $size[1] - $watermark_height - 5; imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100); switch ($file_type) { case "jpg": imagejpeg($image,".." . $base_watermark . "/$new_file_name." . $file_type); break; case "gif": imagegif($image,".." . $base_watermark . "/$new_file_name." . $file_type); break; case "png": imagepng($image,".." . $base_watermark . "/$new_file_name." . $file_type); break; } imagedestroy($image); imagedestroy($watermark); //end watermark where does the script grab the resized image though. because i think the resize one outputs the image as $filename, and i will need the watermark script to do the same. thanks for your help Quote Link to comment Share on other sites More sharing options...
joma Posted December 15, 2007 Author Share Posted December 15, 2007 btw does my path to the watermark have to be relative or can i use www.site.com/blah/blah/watermark Quote Link to comment Share on other sites More sharing options...
joma Posted December 15, 2007 Author Share Posted December 15, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted December 15, 2007 Share Posted December 15, 2007 I think the watermark can be remote imagepng($image,".." . $base_watermark . "/$new_file_name." . $file_type); change these in the switch to imagepng($image); to get the image displayed on screen. Quote Link to comment Share on other sites More sharing options...
joma Posted December 17, 2007 Author Share Posted December 17, 2007 i have tried this as you explained, i am not getting any result. If i post the file, can you help me get the script in there looking like it should work and then i can test it ? I have the resize one working great but can't implement the watermark. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.