PHP實現圖片裁剪與縮放的幾種方法

來源:互聯網
上載者:User

方法一

 代碼如下 複製代碼

圖片裁剪縮放函數,支援方位裁剪和自訂座標裁剪
    /**
     * 圖片裁剪函數,支援指定定點裁剪和方位裁剪兩種裁剪模式
     * @param <string>  $src_file       原圖片路徑
     * @param <int>     $new_width      裁剪後圖片寬度(當寬度超過原圖片寬度時,去原圖片寬度)
     * @param <int>     $new_height     裁剪後圖片高度(當寬度超過原圖片寬度時,去原圖片高度)
     * @param <int>     $type           裁剪方式,1-方位元模式裁剪;0-定點模式裁剪。
     * @param <int>     $pos            方位元模式裁剪時的起始方位(當選定點模式裁剪時,此參數不起作用)
     *                                      1為頂端居左,2為頂端置中,3為頂端居右;
     *                                      4為中部居左,5為中部置中,6為中部居右;
     *                                      7為底端居左,8為底端置中,9為底端居右;
     * @param <int>     $start_x        起始位置X (當選定方位元模式裁剪時,此參數不起作用)
     * @param <int>     $start_y        起始位置Y(當選定方位元模式裁剪時,此參數不起作用)
     * @return <string>                 裁剪圖片儲存路徑
     */
    function thumb($src_file, $new_width, $new_height, $type = 1, $pos = 5, $start_x = 0, $start_y = 0) {
        $pathinfo = pathinfo($src_file);
        $dst_file = $pathinfo['dirname'] . '/' . $pathinfo['filename'] .'_'. $new_width . 'x' . $new_height . '.' . $pathinfo['extension'];
        if (!file_exists($dst_file)) {
            if ($new_width < 1 || $new_height < 1) {
                echo "params width or height error !";
                exit();
            }
            if (!file_exists($src_file)) {
                echo $src_file . " is not exists !";
                exit();
            }
            // 映像類型
            $img_type = exif_imagetype($src_file);
            $support_type = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
            if (!in_array($img_type, $support_type, true)) {
                echo "只支援jpg、png、gif格式圖片裁剪";
                exit();
            }
            /* 載入映像 */
            switch ($img_type) {
                case IMAGETYPE_JPEG :
                    $src_img = imagecreatefromjpeg($src_file);
                    break;
                case IMAGETYPE_PNG :
                    $src_img = imagecreatefrompng($src_file);
                    break;
                case IMAGETYPE_GIF :
                    $src_img = imagecreatefromgif($src_file);
                    break;
                default:
                echo "載入映像錯誤!";
                exit();
            }
            /* 擷取源圖片的寬度和高度 */
            $src_width = imagesx($src_img);
            $src_height = imagesy($src_img);
            /* 計算剪下圖片的寬度和高度 */
            $mid_width = ($src_width < $new_width) ? $src_width : $new_width;
            $mid_height = ($src_height < $new_height) ? $src_height : $new_height;
            /* 初始化源圖片剪下裁剪的起始位置座標 */
            switch ($pos * $type) {
                case 1://1為頂端居左
                    $start_x = 0;
                    $start_y = 0;
                    break;
                case 2://2為頂端置中
                    $start_x = ($src_width - $mid_width) / 2;
                    $start_y = 0;
                    break;
                case 3://3為頂端居右
                    $start_x = $src_width - $mid_width;
                    $start_y = 0;
                    break;
                case 4://4為中部居左
                    $start_x = 0;
                    $start_y = ($src_height - $mid_height) / 2;
                    break;
                case 5://5為中部置中
                    $start_x = ($src_width - $mid_width) / 2;
                    $start_y = ($src_height - $mid_height) / 2;
                    break;
                case 6://6為中部居右
                    $start_x = $src_width - $mid_width;
                    $start_y = ($src_height - $mid_height) / 2;
                    break;
                case 7://7為底端居左
                    $start_x = 0;
                    $start_y = $src_height - $mid_height;
                    break;
                case 8://8為底端置中
                    $start_x = ($src_width - $mid_width) / 2;
                    $start_y = $src_height - $mid_height;
                    break;
                case 9://9為底端居右
                    $start_x = $src_width - $mid_width;
                    $start_y = $src_height - $mid_height;
                    break;
                default://隨機
                    break;
            }
            // 為剪下映像建立背景畫板
            $mid_img = imagecreatetruecolor($mid_width, $mid_height);
            //拷貝剪下的映像資料到畫板,產生剪下映像
            imagecopy($mid_img, $src_img, 0, 0, $start_x, $start_y, $mid_width, $mid_height);
            // 為裁剪映像建立背景畫板
            $new_img = imagecreatetruecolor($new_width, $new_height);
            //拷貝剪下映像到背景畫板,並按比例裁剪
            imagecopyresampled($new_img, $mid_img, 0, 0, 0, 0, $new_width, $new_height, $mid_width, $mid_height);
            /* 按格式儲存為圖片 */
            switch ($img_type) {
                case IMAGETYPE_JPEG :
                    imagejpeg($new_img, $dst_file, 100);
                    break;
                case IMAGETYPE_PNG :
                    imagepng($new_img, $dst_file, 9);
                    break;
                case IMAGETYPE_GIF :
                    imagegif($new_img, $dst_file, 100);
                    break;
                default:
                    break;
            }
        }
        return ltrim($dst_file, '.');
    }

附件是一個有範例圖片的測試demo。
不過,這個函數不支援動態gif圖片的裁剪縮放。

方法二,

 代碼如下 複製代碼

<?php
list($src_w,$src_h)=getimagesize($src_img);  // 擷取原圖尺寸
$dst_scale = $dst_h/$dst_w; //靶心圖表像長寬比
$src_scale = $src_h/$src_w; // 原圖長寬比

if($src_scale>=$dst_scale)

 // 過高
 $w = intval($src_w);
 $h = intval($dst_scale*$w);
 $x = 0;
 $y = ($src_h - $h)/3;
}
else
{
// 過寬
 $h = intval($src_h);
 $w = intval($h/$dst_scale);
 $x = ($src_w - $w)/2;
 $y = 0;
}
// 剪裁
$source=imagecreatefromjpeg($src_img);
$croped=imagecreatetruecolor($w, $h);
imagecopy($croped,$source,0,0,$x,$y,$src_w,$src_h);
// 縮放
$scale = $dst_w/$w;
$target = imagecreatetruecolor($dst_w, $dst_h);
$final_w = intval($w*$scale);
$final_h = intval($h*$scale);
imagecopysampled($target,$croped,0,0,0,0,$final_w,$final_h,$w,$h);
// 儲存
$timestamp = time();
imagejpeg($target, "$timestamp.jpg");
imagedestroy($target);
?>

方法三


根據自訂的靶心圖表, 按靶心圖表高寬比裁剪、縮放源圖.

傳入參數:

 代碼如下 複製代碼

$source_path string 源圖路徑

$target_width integer 靶心圖表寬度

$target_height integer 靶心圖表高度

支援圖片類型: image/gif, image/jpeg, image/png.

function imagecropper($source_path, $target_width, $target_height)
{
 $source_info   = getimagesize($source_path);
 $source_width  = $source_info[0];
 $source_height = $source_info[1];
 $source_mime   = $source_info['mime'];
 $source_ratio  = $source_height / $source_width;
 $target_ratio  = $target_height / $target_width;

 // 源圖過高
 if ($source_ratio > $target_ratio)
 {
  $cropped_width  = $source_width;
  $cropped_height = $source_width * $target_ratio;
  $source_x = 0;
  $source_y = ($source_height - $cropped_height) / 2;
 }
 // 源圖過寬
 elseif ($source_ratio < $target_ratio)
 {
  $cropped_width  = $source_height / $target_ratio;
  $cropped_height = $source_height;
  $source_x = ($source_width - $cropped_width) / 2;
  $source_y = 0;
 }
 // 源圖適中
 else
 {
  $cropped_width  = $source_width;
  $cropped_height = $source_height;
  $source_x = 0;
  $source_y = 0;
 }

 switch ($source_mime)
 {
  case 'image/gif':
   $source_image = imagecreatefromgif($source_path);
   break;

  case 'image/jpeg':
   $source_image = imagecreatefromjpeg($source_path);
   break;

  case 'image/png':
   $source_image = imagecreatefrompng($source_path);
   break;

  default:
   return false;
   break;
 }

 $target_image  = imagecreatetruecolor($target_width, $target_height);
 $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

 // 裁剪
 imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
 // 縮放
 imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

 header('Content-Type: image/jpeg');
 imagejpeg($target_image);
 imagedestroy($source_image);
 imagedestroy($target_image);
 imagedestroy($cropped_image);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.