Jump to content

Create Canvas for image


petenaylor

Recommended Posts

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

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.