源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

PHP实现的自定义图像居中裁剪函数示例【测试可用】

  • 时间:2020-01-16 20:01 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP实现的自定义图像居中裁剪函数示例【测试可用】
本文实例讲述了PHP实现的自定义图像居中裁剪函数。分享给大家供大家参考,具体如下: [b]图像居中裁减的大致思路:[/b] 1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域。(imagecopyresampled — 重采样拷贝部分图像并调整大小) 2.将缩放后的图像放置在裁减区域中间。(imagecopy — 拷贝图像的一部分) 3.裁减图像并保存。(imagejpeg | imagepng | imagegif — 输出图象到浏览器或文件) [b]具体代码:[/b]
//==================缩放裁剪函数====================
/**
 * 居中裁剪图片
 * @param string $source [原图路径]
 * @param int $width [设置宽度]
 * @param int $height [设置高度]
 * @param string $target [目标路径]
 * @return bool [裁剪结果]
 */
function image_center_crop($source, $width, $height, $target)
{
  if (!file_exists($source)) return false;
  /* 根据类型载入图像 */
  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()函数获取返回的布尔值
  return file_exists($target)?true:false;//兼容低版本PHP写法
}

//==================函数使用方式====================
// 原始图片的路径
$source = '../source/img/middle.jpg';
$width = 480; // 裁剪后的宽度
$height = 480;// 裁剪后的高度
// 裁剪后的图片存放目录
$target = '../source/temp/resize.jpg';
// 裁剪后保存到目标文件夹
if (image_center_crop($source, $width, $height, $target)) {
  echo "原图1440*900为:<img src='$source'>";
  echo "<hr>";
    echo "修改后图片480*480为:<img src='$target'>";
}
运行效果: 原图1440*900为:[img]http://files.jb51.net/file_images/article/201708/2017811100340015.jpg?201771110273[/img]
修改后图片480*480为:[img]http://files.jb51.net/file_images/article/201708/2017811102706581.jpg?2017711102745[/img] 同理,480*320,、800*600等尺寸的图片只需修改相应参数即可。 [b]附:代码测试中遇到的问题[/b] [b]报错:[/b]call an undefined function exif_imagetype() [b]解决方法:[/b] 打开扩展 [code]extension=php_exif.dll[/code] 并将[code]extension=php_mbstring.dll[/code] ,放到[code]extension=php_exif.dll[/code]前边 [b]另:[/b][code]boolval()[/code]函数为PHP5.5版本以上才能使用的函数,本文测试代码中为兼容低版本,使用如下语句代替:
return file_exists($target)?true:false;
[b]PS:这里再为大家推荐几款相关的图片在线工具供大家参考使用:[/b] [b]在线图片格式转换(jpg/bmp/gif/png)工具: [/b][url=http://tools.jb51.net/aideddesign/picext]http://tools.jb51.net/aideddesign/picext[/url] [b]在线PS图像处理工具: [/b][url=http://tools.jb51.net/aideddesign/webps]http://tools.jb51.net/aideddesign/webps[/url] [b]ICO图标在线生成工具: [/b][url=http://tools.jb51.net/aideddesign/ico_img]http://tools.jb51.net/aideddesign/ico_img[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/455.htm]PHP图形与图片操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/495.htm]PHP网络编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》及《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部