求php图片缩放处理函数

1000x900的图片500x500的div 按宽等比例缩放至 500x450 当高大于宽时 按高等比例缩放 求老鸟指导!

<?php
/**
 * 图片缩放
 * @param string $url
 * @param int $maxWidth
 * @param int $maxHeight
 * @return string
 */
function thumb($url, $maxWidth, $maxHeight, &$info) {
    $info = $imgInfo = getimagesize($url);
    $width = $imgInfo[0];//获取图片宽度
    $height = $imgInfo[1];//获取图片高度
    $r = min($maxHeight/$height, $maxWidth/$width);
    if($r >= 1) { // 不用缩放
        $maxHeight = $height;
        $maxWidth = $width;
    } elseif($r < 1) { // 缩放
        $maxHeight = $height * $r;
        $maxWidth = $width * $r;
    }
    $temp_img = imagecreatetruecolor($maxWidth,$maxHeight); //创建画布
    $fun = str_replace('/', 'createfrom', $imgInfo['mime']);
    $im = $fun($url);
    imagecopyresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);

    ob_start();
    $fun = str_replace('/', '', $imgInfo['mime']);
    $fun($temp_img);
    $imgstr = ob_get_contents();
    ob_end_clean();
    imagedestroy($im);
    return $imgstr;
}

$imgUrl = $_GET['url'];
$info = array();
$string = thumb($imgUrl, 500, 500, $info);
$mimeArray = explode("/", $info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo $string;

以上代码存为thumb.php,调用效果:

温馨提示:内容为网友见解,仅供参考
第1个回答  2016-07-29
  在PHP网站开发过程中,如果建立的网站涉及大量的图片处理,必然涉及到图片的上传和缩放,保持图片不失真,进行图片缩放。使用之前需要下载安装GD库,以支持PHP图片处理。下面结合代码讲解具体的PHP图片缩放处理的思路。
  function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);

if(($maxwidth && $pic_width > $maxwidth) ($maxheight && $pic_height > $maxheight))
{
if($maxwidth && $pic_width>$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}

if($maxheight && $pic_height>$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}

if($resizewidth_tag && $resizeheight_tag)
{
if($widthratio<$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}

if($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;

$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;

if(function_exists("imagecopyresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}

$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}
  参数说明:
  $im 图片对象,应用函数之前,需要用imagecreatefromjpeg()读取图片对象,如果PHP环境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();
  $maxwidth 定义生成图片的最大宽度(单位:像素)
  $maxheight 生成图片的最大高度(单位:像素)
  $name 生成的图片名
  $filetype 最终生成的图片类型(.jpg/.png/.gif)
  代码注释:
  第3~4行:读取需要缩放的图片实际宽高
  第8~26行:通过计算实际图片宽高与需要生成图片的宽高的压缩比例最终得出进行图片缩放是根据宽度还是高度进行缩放,当前程序是根据宽度进行图片缩放。如果想根据高度进行图片缩放,可以将第22行的语句改成$widthratio>$heightratio
  第28~31行:如果实际图片的长度或者宽度小于规定生成图片的长度或者宽度,则要么根据长度进行图片缩放,要么根据宽度进行图片缩放。
  第33~34行:计算最终缩放生成的图片长宽。
  第36~45行:根据计算出的最终生成图片的长宽改变图片大小,有两种改变图片大小的方法:ImageCopyResized()函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑,但该函数的速度比ImageCopyResized()慢。
  第47~49行:最终生成经过处理后的图片,如果需要生成GIF或PNG,需要将imagejpeg()函数改成imagegif()或imagepng()
  第51~56行:如果实际图片的长宽小于规定生成的图片长宽,则保持图片原样,同理,如果需要生成GIF或PNG,需要将imagejpeg()函数改成imagegif()或imagepng()。
  特别说明:
  GD库1.6.2版以前支持GIF格式,但因GIF格式使用LZW演算法牵涉专利权,因此在GD1.6.2版之后不支持GIF的格式。如果是WINDOWS的环境,只要进入PHP.INI文件找到extension=php_gd2.dll,将#去除,重启APACHE即可。如果是Linux环境,又想支持GIF,PNG,JPEG,需要去下载libpng,zlib,以及freetype字体并安装。
  OK,PHP图片压缩函数完成,最后概述一下整个处理的思路:
  通过计算实际图片的长宽与规定生成图片的长宽之间的缩放比例,根据实际的需求(按照宽度还是按照高度进行图片缩放)计算出最终生成图片的大小,然后应用PHP图片处理函数对图片进行处理,最后输出图片。
  以上就是关于PHP图片处理中如何对图片进行压缩并保持不失真的函数说明。
相似回答
大家正在搜