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

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

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

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

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

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);
  /* 計(jì)算裁剪寬度和高度 */
  $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寫(xiě)法
}

//==================函數(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'>";
}

運(yùn)行效果:

原圖1440*900為:


修改后圖片480*480為:

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

附:代碼測(cè)試中遇到的問(wèn)題

報(bào)錯(cuò):call an undefined function exif_imagetype()

解決方法:

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

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

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

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è)計(jì)入門教程》、《php字符串(string)用法總結(jié)》及《PHP基本語(yǔ)法入門教程

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

相關(guān)文章

最新評(píng)論

荥阳市| 神农架林区| 金乡县| 黄浦区| 达孜县| 合阳县| 侯马市| 道真| 台北市| 博乐市| 阿勒泰市| 黄龙县| 通道| 拉萨市| 青川县| 昆山市| 玉龙| 临高县| 东阳市| 中西区| 扶沟县| 曲沃县| 美姑县| 平邑县| 朝阳县| 九龙县| 竹溪县| 卢龙县| 婺源县| 聂拉木县| 隆昌县| 满洲里市| 南宁市| 丰原市| 济宁市| 资阳市| 理塘县| 买车| 双鸭山市| 镇远县| 观塘区|