I got this script to re size a picture and output it :
<?php
/**
* Produce a preview of the picture
*
*/
class CtrlImagePreview {
const EXT_DOC = 'doc';
const EXT_FILE = 'file';
const EXT_XLS = 'xls';
/**
* Get the appropriate icon in function of the extension
* @param string $ext
*/
public function icon($ext) {
//put the extension in lowercase
$ext = strtolower($ext);
//check if icon exist
if(file_exists('picture/icon/'.$ext.'.png')) {
//display the approriate icon
$url = 'picture/icon/'.$ext.'.png';
} else {
//display
$url = 'picture/icon/file.png';
}
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,time()+60*60*24*30));
header('Content-Type: image/png');
header('Content-Transfer-Encoding: binary');
readfile($url);
}
/**
* Resize and output preview of the image
* @param File $file
*/
public function resize(File $file) {
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,time()+60*60*24*30));
header('Content-Type: image/png');
header('Content-Transfer-Encoding: binary');
$this->image('../upload/'.$_SESSION['c']->getId().'/'.$file->getProject()->getId().'/'.$file->getName(), true, 45);
}
/**
* NOTE: this function has been imported for image resizing, output mime: image/jpeg
* @param unknown_type $image
* @param unknown_type $crop
* @param unknown_type $size
* @return boolean
*/
private function image($image, $crop = null, $size = null) {
$image = ImageCreateFromString(file_get_contents($image));
if (is_resource($image) === true) {
$x = 0;
$y = 0;
$width = imagesx($image);
$height = imagesy($image);
/*
CROP (Aspect Ratio) Section
*/
if (is_null($crop) === true) {
$crop = array($width, $height);
} else {
$crop = array_filter(explode(':', $crop));
if (empty($crop) === true) {
$crop = array($width, $height);
} else {
if ((empty($crop[0]) === true) || (is_numeric($crop[0]) === false)) {
$crop[0] = $crop[1];
} else if ((empty($crop[1]) === true) || (is_numeric($crop[1]) === false)) {
$crop[1] = $crop[0];
}
}
$ratio = array(0 => $width / $height, 1 => $crop[0] / $crop[1]);
if ($ratio[0] > $ratio[1]) {
$width = $height * $ratio[1];
$x = (imagesx($image) - $width) / 2;
}
else if ($ratio[0] < $ratio[1]) {
$height = $width / $ratio[1];
$y = (imagesy($image) - $height) / 2;
}
}
/*
Resize Section
*/
if (is_null($size) === true) {
$size = array($width, $height);
}
else {
$size = array_filter(explode('x', $size));
if (empty($size) === true) {
$size = array(imagesx($image), imagesy($image));
} else {
if ((empty($size[0]) === true) || (is_numeric($size[0]) === false)) {
$size[0] = round($size[1] * $width / $height);
} else if ((empty($size[1]) === true) || (is_numeric($size[1]) === false)) {
$size[1] = round($size[0] * $height / $width);
}
}
}
$result = ImageCreateTrueColor($size[0], $size[1]);
if (is_resource($result) === true) {
ImageSaveAlpha($result, true);
ImageAlphaBlending($result, true);
ImageFill($result, 0, 0, ImageColorAllocate($result, 255, 255, 255));
ImageCopyResampled($result, $image, 0, 0, $x, $y, $size[0], $size[1], $width, $height);
ImageInterlace($result, true);
Imagepng($result, null, 0);
}
}
return false;
}
}
?>
The problem it’s that this script is sometimes working sometimes not, and it seems to be not working on big image (only on my server, in local mode it is fine!).
Here is the pictures generated by this script:

It might not be obvious, but when manipulating images with PHP functions, the image gets uncompressed in the memory. If you have a 1 MB JPEG photo with the resolution of 2500×1500 (roughly a 4 MPix picture), the uncompressed size is 2500 × 1500 (resolution) × 4 (bytes per pixel) = 15 MB. With even more compressed JPEGs, the ratio between compressed vs. uncompressed data can easily be 1:30 or even bigger. And for correct image manipulation, you might even need twice this amount of available memory (for input image and for the output).
The usual solution is to either increase the memory limit (in PHP’s configuration
memory_limit) or to use an external library, like ImageMagick.