The correct method is to create a new image and then copy the old image to the middle (provided that the initial image is JPEG and less 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, $old, $oldh);
source share