最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP實現(xiàn)的自定義圖像居中裁剪函數(shù)示例【測試可用】

 更新時間:2017年08月11日 10:29:56   作者:woider  
這篇文章主要介紹了PHP實現(xiàn)的自定義圖像居中裁剪函數(shù),結(jié)合實例形式分析了php針對圖片的獲取、計算、裁剪、保存等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了PHP實現(xiàn)的自定義圖像居中裁剪函數(shù)。分享給大家供大家參考,具體如下:

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

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

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'>";
}

運行效果:

原圖1440*900為:


修改后圖片480*480為:

同理,480*320,、800*600等尺寸的圖片只需修改相應(yīng)參數(shù)即可。

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

報錯:call an undefined function exif_imagetype()

解決方法:

打開擴展 extension=php_exif.dll

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

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

return file_exists($target)?true:false;

PS:這里再為大家推薦幾款相關(guān)的圖片在線工具供大家參考使用:

在線圖片格式轉(zhuǎn)換(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext

在線PS圖像處理工具:
http://tools.jb51.net/aideddesign/webps

ICO圖標(biāo)在線生成工具:
http://tools.jb51.net/aideddesign/ico_img

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》及《PHP基本語法入門教程

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論

临汾市| 铜川市| 连云港市| 龙州县| 陆川县| 通州市| 封开县| 砚山县| 西丰县| 文水县| 祁东县| 大足县| 县级市| 梧州市| 民丰县| 青海省| 临澧县| 宝应县| 阿巴嘎旗| 荆门市| 新巴尔虎左旗| 浦北县| 万荣县| 岢岚县| 会宁县| 封开县| 贡山| 周宁县| 灵台县| 武威市| 海阳市| 南康市| 乌鲁木齐市| 神农架林区| 咸丰县| 平顶山市| 罗城| 景谷| 大安市| 枣阳市| 吉安县|