PHP实现图片不变型裁剪及图片按比例裁剪的方法

 更新时间:2016年01月14日 14:45:18   作者:乘着风在飞  
这篇文章主要介绍了PHP实现图片不变型裁剪及图片按比例裁剪的方法,涉及PHP裁剪缩略图的常用技巧,需要的朋友可以参考下

本文实例讲述了PHP实现图片不变型裁剪及图片按比例裁剪的方法。分享给大家供大家参考,具体如下:

图片不变型裁剪

<?php
/**
 * imageCropper
 * @param string $source_path
 * @param string $target_width
 * @param string $target_height
 */
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){
    // image-to-height
    $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){
    //image-to-widht
    $cropped_width = $source_height / $target_ratio;
    $cropped_height = $source_height;
    $source_x = ($source_width - $cropped_width) / 2;
    $source_y = 0;
  }else{
    //image-size-ok
    $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 ;
      break;
  }
  $target_image = imagecreatetruecolor($target_width, $target_height);
  $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
  // copy
  imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
  // zoom
  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);
}
$filename = "8fcb7a0831b79c61.jpg";
imageCropper($filename,200,200);
?>

图片按比例裁剪

<?php
/**
 * imageZoom
 * @param string $file
 * @param double $zoom
 */
function imageZoom($filename,$zoom=0.6){
  //baseinfo
  $sourceImageInfo = getimagesize($filename);
  $sourceWidth = $sourceImageInfo[0];
  $sourceHeight = $sourceImageInfo[1];
  $sourceMine = $sourceImageInfo['mime'];
  $sourceRatio = $sourceWidth/$sourceHeight;
  $sourceX = 0;
  $sourceY = 0;
  //zoom
  $targetRatio = $zoom;
  //target-widht-height
  $targetWidth = $sourceWidth*$targetRatio;
  $targetHeight = $sourceHeight*$targetRatio;
  //init-params
  $sourceImage = null;
  switch($sourceMine){
    case 'image/gif':
      $sourceImage = imagecreatefromgif($filename);
      break;
    case 'image/jpeg':
      $sourceImage = imagecreatefromjpeg($filename);
      break;
    case 'image/png':
      $sourceImage = imagecreatefrompng($filename);
      break;
    default:
      return ;
      break;
  }
  //temp-target-image
  $tempSourceImage = imagecreatetruecolor($sourceWidth, $sourceHeight);
  $targetImage = imagecreatetruecolor($targetWidth,$targetHeight);
  //copy
  imagecopy($tempSourceImage, $sourceImage, 0, 0, $sourceX, $sourceY, $sourceWidth, $sourceHeight);
  //zoom
  imagecopyresampled($targetImage, $tempSourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
  //header
  header('Content-Type: image/jpeg');
  //image-loading
  imagejpeg($targetImage);
  //destroy
  imagedestroy($tempSourceImage);
  imagedestroy($sourceImage);
  imagedestroy($targetImage);
}
$filename = "8fcb7a0831b79c61.jpg";
imageZoom($filename);
?>

希望本文所述对大家PHP程序设计有所帮助。

相关文章

  • php中时间轴开发(刚刚、5分钟前、昨天10:23等)

    php中时间轴开发(刚刚、5分钟前、昨天10:23等)

    php中时间轴开发,即显示为“刚刚”、“5分钟前”、“昨天10:23”等
    2011-10-10
  • PHP多线程类及用法实例

    PHP多线程类及用法实例

    这篇文章主要介绍了PHP多线程类及用法,实例分析了多线程类的具体实现方法及应用技巧,并结合下载远程图片的实例予以深入分析,需要的朋友可以参考下
    2014-12-12
  • php获取网页上所有链接的方法

    php获取网页上所有链接的方法

    这篇文章主要介绍了php获取网页上所有链接的方法,涉及php操作正则匹配的技巧,代码简单实用,需要的朋友可以参考下
    2015-04-04
  • PHP实现无限极分类生成分类树的方法

    PHP实现无限极分类生成分类树的方法

    这篇文章主要介绍了PHP实现无限极分类生成分类树的方法,结合实例形式简单分析了无限极分类的原理与实现方法,涉及PHP数组遍历与判断相关操作技巧,需要的朋友可以参考下
    2017-09-09
  • 如何使用PHP计算上一个月的今天

    如何使用PHP计算上一个月的今天

    本篇文章是对用PHP计算上一个月的今天的实例进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • discuz的php防止sql注入函数

    discuz的php防止sql注入函数

    最早开始学习php的时候根本没考虑过安全方面的问题,那时候就是想能做出功能就是万岁了。随着做项目的时间慢慢加长,越来越感觉到网站安全方面的问题十分重要。
    2011-01-01
  • PHP合并数组函数array_merge用法分析

    PHP合并数组函数array_merge用法分析

    这篇文章主要介绍了PHP合并数组函数array_merge用法,结合实例形式分析了php数组合并函数array_merge的具体功能、使用方法与相关注意事项,需要的朋友可以参考下
    2017-02-02
  • 详解php中curl返回false的解决办法

    详解php中curl返回false的解决办法

    这篇文章主要介绍了php中curl返回false的解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • PHP二维数组排序简单实现方法

    PHP二维数组排序简单实现方法

    这篇文章主要介绍了PHP二维数组排序简单实现方法,涉及PHP针对数组的遍历与排序操作常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-02-02
  • PHP echo,print,printf,sprintf函数之间的区别与用法详解

    PHP echo,print,printf,sprintf函数之间的区别与用法详解

    这篇文章主要是对PHP中echo,print,printf,sprintf函数之间的区别与用法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2013-11-11

最新评论