我得到了这个脚本来调整图片大小并输出它:
<?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;
}
}
?>
问题是这个脚本有时工作有时不工作,而且它似乎不适用于大图像(仅在我的服务器上,在本地模式下很好!)。
这是此脚本生成的图片: