溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在PHP中自定義一個圖像居中裁剪函數(shù)

發(fā)布時間:2021-04-15 16:10:24 來源:億速云 閱讀:213 作者:Leah 欄目:開發(fā)技術(shù)

怎么在PHP中自定義一個圖像居中裁剪函數(shù)?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

圖像居中裁減的大致思路:

1.首先將圖像進(jìn)行縮放,使得縮放后的圖像能夠恰好覆蓋裁減區(qū)域。(imagecopyresampled — 重采樣拷貝部分圖像并調(diào)整大?。?/p>

2.將縮放后的圖像放置在裁減區(qū)域中間。(imagecopy — 拷貝圖像的一部分)

3.裁減圖像并保存。(imagejpeg | imagepng | imagegif — 輸出圖象到瀏覽器或文件)

具體代碼:

//==================縮放裁剪函數(shù)====================
/**
 * 居中裁剪圖片
 * @param string $source [原圖路徑]
 * @param int $width [設(shè)置寬度]
 * @param int $height [設(shè)置高度]
 * @param string $target [目標(biāo)路徑]
 * @return bool [裁剪結(jié)果]
 */
function image_center_crop($source, $width, $height, $target)
{
  if (!file_exists($source)) return false;
  /* 根據(jù)類型載入圖像 */
  switch (exif_imagetype($source)) {
    case IMAGETYPE_JPEG:
      $image = imagecreatefromjpeg($source);
      break;
    case IMAGETYPE_PNG:
      $image = imagecreatefrompng($source);
      break;
    case IMAGETYPE_GIF:
      $image = imagecreatefromgif($source);
      break;
  }
  if (!isset($image)) return false;
  /* 獲取圖像尺寸信息 */
  $target_w = $width;
  $target_h = $height;
  $source_w = imagesx($image);
  $source_h = imagesy($image);
  /* 計算裁剪寬度和高度 */
  $judge = (($source_w / $source_h) > ($target_w / $target_h));
  $resize_w = $judge ? ($source_w * $target_h) / $source_h : $target_w;
  $resize_h = !$judge ? ($source_h * $target_w) / $source_w : $target_h;
  $start_x = $judge ? ($resize_w - $target_w) / 2 : 0;
  $start_y = !$judge ? ($resize_h - $target_h) / 2 : 0;
  /* 繪制居中縮放圖像 */
  $resize_img = imagecreatetruecolor($resize_w, $resize_h);
  imagecopyresampled($resize_img, $image, 0, 0, 0, 0, $resize_w, $resize_h, $source_w, $source_h);
  $target_img = imagecreatetruecolor($target_w, $target_h);
  imagecopy($target_img, $resize_img, 0, 0, $start_x, $start_y, $resize_w, $resize_h);
  /* 將圖片保存至文件 */
  if (!file_exists(dirname($target))) mkdir(dirname($target), 0777, true);
  switch (exif_imagetype($source)) {
    case IMAGETYPE_JPEG:
      imagejpeg($target_img, $target);
      break;
    case IMAGETYPE_PNG:
      imagepng($target_img, $target);
      break;
    case IMAGETYPE_GIF:
      imagegif($target_img, $target);
      break;
  }
//  return boolval(file_exists($target));//PHP5.5以上可用boolval()函數(shù)獲取返回的布爾值
  return file_exists($target)?true:false;//兼容低版本PHP寫法
}
//==================函數(shù)使用方式====================
// 原始圖片的路徑
$source = '../source/img/middle.jpg';
$width = 480; // 裁剪后的寬度
$height = 480;// 裁剪后的高度
// 裁剪后的圖片存放目錄
$target = '../source/temp/resize.jpg';
// 裁剪后保存到目標(biāo)文件夾
if (image_center_crop($source, $width, $height, $target)) {
  echo "原圖1440*900為:<img src='$source'>";
  echo "<hr>";
    echo "修改后圖片480*480為:<img src='$target'>";
}

附:代碼測試中遇到的問題

報錯:call an undefined function exif_imagetype()

解決方法:

打開擴(kuò)展 extension=php_exif.dll

并將extension=php_mbstring.dll ,放到extension=php_exif.dll前邊

另:boolval()函數(shù)為PHP5.5版本以上才能使用的函數(shù),本文測試代碼中為兼容低版本,使用如下語句代替:

return file_exists($target)?true:false;

看完上述內(nèi)容,你們掌握怎么在PHP中自定義一個圖像居中裁剪函數(shù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI