我正在为数字资产管理器创建缩略图,使用imagemagick最好的方法是什么?
有没有好的资源可以参考?
我正在为数字资产管理器创建缩略图,使用imagemagick最好的方法是什么?
有没有好的资源可以参考?
我已经解决了这个问题,并将与全世界分享!它可以将 .ai、.psd、.jpg、.png 和 .gif 转换为缩略图。
这里有一个函数,它接受 4 个参数:
$dir - 保存到的目录。
$tmpName - 指定文件名但不包括扩展名。
$fileType - 文件类型,不用解释。
$size - 大或小。
function thumbGenerator($dir,$tmpName,$fileType,$size){
$saveFileType = "png";
$imagePath = $dir.$tmpName.".".$fileType;
$image = new Imagick();
$image->readimage($imagePath);
if($fileType == "psd"){
$image->setIteratorIndex(0);
}
$dimensions = $image->getImageGeometry();
$width = $dimensions['width'];
$height = $dimensions['height'];
if($size == "large"){
$maxWidth = 720;
$maxHeight =720;
}
if($size == "small"){
$maxWidth = 250;
$maxHeight =250;
}
if($height > $width){
//Portrait
if($height > $maxHeight)
$image->thumbnailImage(0, $maxHeight);
$dimensions = $image->getImageGeometry();
if($dimensions['width'] > $maxWidth){
$image->thumbnailImage($maxWidth, 0);
}
}elseif($height < $width){
//Landscape
$image->thumbnailImage($maxWidth, 0);
}else{
//square
$image->thumbnailImage($maxWidth, 0);
}
if($size == "large"){
$image->writeImage($dir . $tmpName."-lg.".$saveFileType);
}
if($size == "small"){
$image->writeImage($dir . $tmpName."-sm.".$saveFileType);;
}
}
@Jason - 感谢分享。以下是几个使代码更清洁、易于维护/扩展的提示。另外,很多取决于您的需求。另外,我没有实际运行这段代码,敬请原谅任何拼写错误。
$dir - 要保存到的目录。
$tmpName - 文件名,不包括扩展名。
$fileType - 自我说明。
$size - 大图或小图。 您可以考虑使用像素宽度值来生成缩略图,而不是预定义宽度字符串。假设您将来需要在页面的新部分中使用更大的缩略图(即具有“小”缩略图的500px的Retina-ready图标),最好在新代码部分中定义大小,而不是在共享的thumbGenerator函数中定义
function thumbGenerator($dir,$tmpName,$fileType,$size){
$saveFileType = "png";
$imagePath = $dir.$tmpName.".".$fileType;
$image = new Imagick();
$image->readimage($imagePath);
if($fileType == "psd"){
$image->setIteratorIndex(0);
}
/* Simplify this code section below
$dimensions = $image->getImageGeometry();
$width = $dimensions['width'];
$height = $dimensions['height'];
*/
list($width,$height) = $image->getImageGeometry(); // <--- new code
/* Use $size for the pixel width/height instead and remove the code below
if($size == "large"){
$maxWidth = 720;
$maxHeight =720;
}
if($size == "small"){
$maxWidth = 250;
$maxHeight =250;
}
*/
if($height > $width){
//Portrait
if($height > $size)
$image->thumbnailImage(0, $size);
$dimensions = $image->getImageGeometry();
if($width > $size){ // <--- use the previously created $width variable
$image->thumbnailImage($size, 0);
}
/* Don't need this duplicate code.
}elseif($height < $width){
//Landscape
$image->thumbnailImage($maxWidth, 0);
*/
}else{
// square or landscape
$image->thumbnailImage($maxWidth, 0);
}
/* DRY - do not repeat yourself - Simplify it and use the pixel width in the image name
if($size == "large"){
$image->writeImage($dir . $tmpName."-lg.".$saveFileType);
}
if($size == "small"){
$image->writeImage($dir . $tmpName."-sm.".$saveFileType);;
}
*/
$image->writeImage($dir . $tmpName."-".$size.".".$saveFileType);;
}
list($width, $height) = $image->getImageGeometry()
这一部分。因为结果不是大小为2的数组,而是关联数组array('width'=>23, 'height'=>42)
。 - psycho brm
convert
命令。尝试使用convert orig.psd output.jpg
命令,看看它是否能够执行 - 如果可以,那么您可以开始尝试调整大小选项。如果不能执行,则不要浪费时间在错误的方向上努力。 - Marc B