有GD格式及ImageMagick縮圖方式
| /** | |
| * GD縮圖 | |
| * $dir - 原始目錄 | |
| * $file - 檔案名稱 | |
| * $size - 最終的大小 | |
| */ | |
| function squareThumbnailByGd($dir, $file, $size) { | |
| $ratioSize = 120; // 預先縮圖,無論圖片大小,先強制縮成指定像素為基準的大小 | |
| $realPosition = $dir . $file; // 圖片加路徑 | |
| $thumbDir = $dir . 'thumbnails/'; // 縮圖目錄 | |
| if (file_exists($realPosition)) { | |
| $fileName = current(explode('.', $file)); // 檔名 | |
| $ext = strtolower(end(explode('.', $realPosition))); // 副檔名 | |
| $newName = $fileName . '_' . $size . 'x' . $size . '_byGD.' . 'jpg'; //新檔名 | |
| // 如果該縮圖存在則直接出圖 | |
| if (file_exists($thumbDir . $newName)) { | |
| echo '<img src="' . $thumbDir . $newName . '" />'; | |
| return; | |
| } | |
| switch ($ext) { | |
| case 'jpg': | |
| case 'jpeg': | |
| $src = imagecreatefromjpeg($realPosition); | |
| break; | |
| case 'gif': | |
| $src = imagecreatefromgif($realPosition); | |
| break; | |
| case 'png': | |
| $src = imagecreatefrompng($realPosition); | |
| break; | |
| } | |
| $srcW = imagesx($src); // 原始寬度 | |
| $srcH = imagesy($src); // 原始高度 | |
| if ($srcW >= $srcH) { | |
| // 以高來等比例縮第一次圖 | |
| $newW = ceil($srcW / $srcH * $ratioSize); // 新寬度 | |
| $newH = $ratioSize; // 新高度 | |
| } else { | |
| // 以寬來等比例縮第一次圖 | |
| $newW = $ratioSize; // 新寬度 | |
| $newH = ceil($srcH / $srcW * $ratioSize); // 新高度 | |
| } | |
| // 縮第一次圖 | |
| $im = imagecreatetruecolor($newW, $newH); | |
| imagecopyresampled($im, $src, 0, 0, 0, 0, $newW, $newH, $srcW, $srcH); | |
| // 縮需求大小的圖 | |
| $im2 = imagecreatetruecolor($size, $size); | |
| $coordX = ($newW - $size) / 2; | |
| $coordY = ($newH - $size) / 2; | |
| imagecopyresampled($im2, $im, 0, 0, $coordX, $coordY, $newW, $newH, $newW, $newH); | |
| imagejpeg($im2, $thumbDir . $newName, 100); //輸出 | |
| imagedestroy($im); | |
| imagedestroy($im2); | |
| echo '<img src="' . $thumbDir . $newName . '" />'; | |
| } | |
| } |
| /** | |
| * ImageMagick縮圖 | |
| * $dir - 原始目錄 | |
| * $file - 檔案名稱 | |
| * $size - 最終的大小 | |
| */ | |
| function squareThumbnailByImageMagick($dir, $file, $size) { | |
| $realPath = $dir . $file; | |
| $ratioSize = 120; // 預先縮圖,無論圖片大小,先強制縮成指定像素為基準的大小 | |
| $realPosition = $dir . $file; // 圖片加路徑 | |
| $thumbDir = $dir . 'thumbnails/'; // 縮圖目錄 | |
| if (file_exists($realPosition)) { | |
| $fileName = current(explode('.', $file)); // 檔名 | |
| $ext = strtolower(end(explode('.', $realPosition))); // 副檔名 | |
| $newName = $fileName . '_' . $size . 'x' . $size . '_byImageMagick.' . 'jpg'; //新檔名 | |
| // 如果該縮圖存在則直接出圖 | |
| if (file_exists($thumbDir . $newName)) { | |
| echo '<img src="' . $thumbDir . $newName . '" />'; | |
| return; | |
| } | |
| $theImg = getimagesize($realPosition); | |
| $srcW = $theImg[0]; // 原始圖片寬度 | |
| $srcH = $theImg[1]; // 原始圖片高度 | |
| if ($srcW >= $srcH) { | |
| // 以高來等比例縮第一次圖 | |
| $newW = ceil($srcW / $srcH * $ratioSize); // 新寬度 | |
| $newH = $ratioSize; // 新高度 | |
| } else { | |
| // 以寬來等比例縮第一次圖 | |
| $newW = $ratioSize; // 新寬度 | |
| $newH = ceil($srcH / $srcW * $ratioSize); // 新高度 | |
| } | |
| $coordX = ($newW - $size) / 2; // crop X 座標 | |
| $coordY = ($newH - $size) / 2; // crop Y 座標 | |
| $exe = 'im_convert -geometry ' . $newW . 'x' . $newH . ' -crop ' . $size . 'x' . $size . '+' . $coordX . '+' . $coordY . ' ' . $realPath . ' ' . $thumbDir . $newName; | |
| exec($exe); | |
| echo '<img src="' . $thumbDir . $newName . '" />'; | |
| } | |
| } |
全站熱搜
留言列表

