petenaylor Posted April 4, 2011 Share Posted April 4, 2011 Hi all I am trying to write a piece of code to take an image, resize it and centre it on a canvas 300 pixels tall by 400 pixels high. I know I need to use imagecopymerged but how do I add it to the below code: $imagelarge = $_FILES['image']['tmp_name']; $imagelargemain = $_FILES['image']['name']; $src = imagecreatefromjpeg($imagelarge); list($width,$height)=getimagesize($imagelarge); $newwidth=300; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); $filename = WEB_UPLOAD."images/right-images/". $_FILES['image']['name']; imagejpeg($tmp,$filename,100); imagedestroy($src); imagedestroy($tmp); Many thanks for your help! Pete Link to comment https://forums.phpfreaks.com/topic/232689-create-canvas-for-image/ Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 Maybe... $imagelarge = $_FILES['image']['tmp_name']; $imagelargemain = $_FILES['image']['name']; $src = imagecreatefromjpeg($imagelarge); list($width,$height)=getimagesize($imagelarge); $newwidth=300; $newheight=($height/$width)*$newwidth; $center_x = 300/2; $center_y = (400/2)-($newheight/2); $tmp=imagecreatetruecolor(300,400); imagecopyresampled($tmp,$src,$center_x,$center_y,0,0,$newwidth,$newheight,$width,$height); $filename = WEB_UPLOAD."images/right-images/". $_FILES['image']['name']; imagejpeg($tmp,$filename,100); imagedestroy($src); imagedestroy($tmp); Link to comment https://forums.phpfreaks.com/topic/232689-create-canvas-for-image/#findComment-1196824 Share on other sites More sharing options...
petenaylor Posted April 4, 2011 Author Share Posted April 4, 2011 Wow, thanks for your quick reply! How would I modify it to get a white background? Link to comment https://forums.phpfreaks.com/topic/232689-create-canvas-for-image/#findComment-1196828 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 Sure thing. Just add $white = imagecolorallocate($tmp, 255, 255, 255); imagefill($tmp, 0, 0, $white); right after $tmp=imagecreatetruecolor(300,400); Link to comment https://forums.phpfreaks.com/topic/232689-create-canvas-for-image/#findComment-1196847 Share on other sites More sharing options...
petenaylor Posted April 4, 2011 Author Share Posted April 4, 2011 Hi there That is great! Many thanks! Link to comment https://forums.phpfreaks.com/topic/232689-create-canvas-for-image/#findComment-1196852 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.