回答
我解決了它,將與世界分享!它會將.ai,.psd,.jpg,.png,.gif轉換爲縮略圖。
這裏是一個函數,採用4個PARAMS:
$ DIR - 目錄保存到。
$ tmpName - 命名文件的名稱,不包括擴展名。
$ fileType - 不言自明。
$尺寸 - 大或小。
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 - 不言自明。
$尺寸 - 大或小。 您可以考慮縮略圖的像素寬度值,而不是預定義寬度的字符串。假設您將來需要在網頁的新部分放置更大的縮略圖(例如,對於「小」縮略圖,帶有500px的視網膜就緒圖標)。應該優選地限定在代碼的新部分的尺寸,而不是在共享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的數組,而是關聯的數組('width'=> 23,'height'=> 42)' –
- 1. 使用ANT將PSD轉換爲PNG/JPG
- 2. PHP - 使用Imagick將EPS轉換爲PNG
- 3. 將PSD轉換爲HTML
- 4. 將PSD轉換爲網站
- 5. 如何將.psd或.ai文件轉換爲多種尺寸的PNG?
- 6. PSD轉換爲XHTML/CSS轉換
- 7. PSD轉換爲html格式
- 8. 將ImageMagick CLI轉換爲PHP Imagick API
- 9. php imagick將PNG轉換爲jpg
- 10. 如何將.ai文件轉換爲PNG
- 11. 如何將ai轉換爲cdr for linux
- 12. 如何使用Ghostscript將psd(photoshop文件)或tiff轉換爲PNG?
- 13. 使用ImageMagick將PSD轉換爲各種Android密度圖像
- 14. 使用Imagick將圖像從RGB轉換爲CMYK
- 15. 使用php imagick將圖像轉換爲草圖
- 16. ImageMagick/Imagick使用原生PHP API將PDF轉換爲JPG
- 17. 如何將文本轉換爲使用php的imagick的圖像?
- 18. 使用Imagick將rgb圖像轉換爲cmyk
- 19. 使用imagick將PDF轉換爲使用imagick的JPG使用flat PDF工作,但在多層時失敗
- 20. 使用Imagick轉換透明PDF文件
- 21. imagick將png轉換爲jpg,在java中用im4java
- 22. PHP Imagick CMYK PDF轉換爲JPG
- 23. 如何將psd中的燈光效果轉換爲xhtml?
- 24. 將psd轉換爲PNG給通過imagemagick給垃圾輸出
- 25. 將PSD轉換爲Android XML(佈局)格式?
- 26. 如何將PSD設計轉換爲Android xml?
- 27. 我應該如何將此PSD轉換爲HTML&CSS菜單
- 28. 如何將此對象從.PSD轉換爲HTML/CSS?
- 29. 如何使用PHP中的Imagick庫將PDF文件轉換爲圖像?
- 30. 使用Imagick php擴展將多頁PDF轉換爲JPG時的黑色背景
命令行的ImageMagick具有'convert'。嘗試'convert orig.psd output.jpg',看看它是否會這樣做 - 如果是這樣,那麼你可以開始搞亂調整大小的選項。如果沒有,那麼你就不會浪費時間咆哮錯誤的樹。 –
有沒有辦法從php(imagick)做命令行的東西? –
這就是爲什麼你可以測試轉換,看看它是否會工作。一個簡單的命令v.s.幾個小時試圖轟炸一個PHP腳本來做同樣的事情。 –