View Full Version : Image scaling with PHP?

21 May 2010, 07:34 PM
Is it possible to scale an image with PHP? I would like to create a script that automatically takes a huge .jpg image and turns it into a smaller, more easily loaded (no larger than 80-120k) image.

21 May 2010, 07:51 PM
yes, you have to use the gd library: http://us3.php.net/manual/en/ref.image.php

Cited From: http://us3.php.net/manual/en/function.imagecopyresized.php

// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg'); // ADDED BY ME: only if you don't want to save it for dynamic image creation only.(use: <img src="resizeimg.php?img=test.jpg" />)

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb); // ADDED BY ME: for dynamic images, to save use imagejpeg($thumb, "folder/location/", 100);