Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP GD - resizing image frame/canvas but not the actual image

I have an image with size 88x31 and would like to make it 100x100 without resizing the actual image but only its canvas/frame, maybe by copying it in the center of the new blank white image with 100x100 size. Any ideas how to do it?

like image 917
Marcin Avatar asked Jan 06 '12 10:01

Marcin


1 Answers

The correct method is to create a new image and then copy the old image into the middle of it (assuming the starting image is is a JPEG and smaller than 100x100):

$oldimage = imagecreatefromjpeg($filename);
$oldw = imagesx($oldimage);
$oldh = imagesy($oldimage);

$newimage = imagecreatetruecolor(100, 100); // Creates a black image

// Fill it with white (optional)
$white = imagecolorallocate($newimage, 255, 255, 255);
imagefill($newimage, 0, 0, $white);

imagecopy($newimage, $oldimage, (100-$oldw)/2, (100-$oldh)/2, 0, 0, $oldw, $oldh);
like image 199
Kevin Borders Avatar answered Sep 18 '22 12:09

Kevin Borders