我需要帮助一个朋友制作自己的艺术画廊。我已经完成了所有工作,但我需要一个工具/插件/脚本来让他独立于我上传他自己的图像。我的画廊需要两张图片:一张按一定比例裁剪的(所以我需要他在上传页面中自己裁剪)和一张拇指(我希望自动完成)。
你知道一个简单的方法来做到这一点吗?你会怎么做?
谢谢。
您只需要 GD 库,函数 imagecopyresampled 将适合您。PHP 手册有很好的缩略图创建示例代码http://php.net/manual/en/function.imagecopyresampled.php 您只需要为不同的文件格式创建例外
function create_jpeg_thumbnail($thumbImageName,$imgSrc,$thumbDirectory,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource.
$thumbDirectory = trim($thumbDirectory);
$imageSourceExploded = explode('/', $imgSrc);
$imageName = $imageSourceExploded[count($imageSourceExploded)-1];
$imageDirectory = str_replace($imageName, '', $imgSrc);
$filetype = explode('.',$imageName);
$filetype = strtolower($filetype[count($filetype)-1]);
//getting the image dimensions
list($width_orig, $height_orig) = getimagesize($imgSrc);
//$myImage = imagecreatefromjpeg($imgSrc);
if ($filetype == 'jpg') {
$myImage = imagecreatefromjpeg("$imageDirectory/$imageName");
} else
if ($filetype == 'jpeg') {
$myImage = imagecreatefromjpeg("$imageDirectory/$imageName");
} else
if ($filetype == 'png') {
$myImage = imagecreatefrompng("$imageDirectory/$imageName");
} else
if ($filetype == 'gif') {
$myImage = imagecreatefromgif("$imageDirectory/$imageName");
}
$ratio_orig = $width_orig/$height_orig;
if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
$new_height = $thumbnail_width/$ratio_orig;
$new_width = $thumbnail_width;
} else {
$new_width = $thumbnail_height*$ratio_orig;
$new_height = $thumbnail_height;
}
$x_mid = $new_width/2; //horizontal middle
$y_mid = $new_height/2; //vertical middle
$process = imagecreatetruecolor(round($new_width), round($new_height));
imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($thumb, $process, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
//$thumbImageName = 'thumb_'.get_random_no().'.jpeg';
$destination = $thumbDirectory=='' ? $thumbImageName : $thumbDirectory."/".$thumbImageName;
imagejpeg($thumb, $destination, 100);
return $thumbImageName;
}
如果您不打算开始有大量流量 - 查看http://phpthumb.sourceforge.net/可以即时创建调整大小的图像。
我个人在我的所有项目中都使用它 - http://www.verot.net/php_class_upload.htm 与上传文件和系统上已有的文件完美配合。
可以做很多事情以多种方式转换,调整大小和处理上传的图像,应用效果,添加标签,水印和反射以及其他图像编辑功能。
易于使用。