php的gd库使用程度,php GD库的使用

/**

* 调整图片大小

* $path图片的宽度大于$width则等比缩放

* $path图片的高度大于$height切成多张图片

* @param $path 图片地址

* @param $width integer|string  图片宽度

* @param @height integer|string 图片高度

*/

function imgResize($path, $width, $height) {

// 存放生成的图片路径

$imgPaths = array();

// 获取图片宽度/高度/类型

list($srcW, $srcH, $type) = getimagesize($path);

$type -= 1;

// 创建源图像

$imagecreatefromFuns = array('imagecreatefromgif', 'imagecreatefromjpeg', 'imagecreatefrompng');

$src = $imagecreatefromFuns[$type]($path);

// 图片的宽度大于$width则等比缩放

if( $srcW > $width ) {

$dstHeight = $srcH * ($width / $srcW);

// 新建一个真彩***像

$dst = imagecreatetruecolor( $width, $dstHeight );

imagecopyresampled($dst, $src,  0, 0, 0, 0, $width, $dstHeight, $srcW, $srcH);

$src = $dst;

// 更新原图的宽度和高度

$srcW = $width;

$srcH = $dstHeight;

}

else {

// 没有调整宽度

$width = $srcW;

}

// 图片的高度大于$height切成多张图片

if( $srcH > $height ) {

// 切割的高度位置

$srcY = 0;

do {

$dstHeight = ($srcY + $height > $srcH) ? ($srcH - $srcY) : ($height);

$dst = imagecreatetruecolor( $width, $dstHeight );

imagecopyresampled($dst, $src,  0, 0, 0, $srcY, $width, $dstHeight, $width, $dstHeight);

$filePath = './img/' . md5(microtime(true)) . '.jpg';

$imgPaths[] = $filePath;

ImageJpeg($dst, $filePath);

is_resource($dst) and imagedestroy($dst);

$srcY += $dstHeight;

}while($srcY 

}

else {

// 如果高度符合 则将调整了原图(也有可能是调整了宽度之后的)保存为文件

$filePath = './img/' . md5(microtime(true)) . '.jpg';

$imgPaths[] = $filePath;

ImageJpeg($src, $filePath);

}

// 释放内存

isset($src) and is_resource($src) and imagedestroy($src);

isset($dst) and is_resource($dst) and imagedestroy($dst);

return $imgPaths;

}

/**

* 调整图片清晰度

* @param string $path  图片路径

* @param string $quality 清晰度

* @return string 新的图片路径

*/

function reQuality( $path, $quality ) {

$image_type = image_type_to_extension(exif_imagetype($path), false);

$fun = "imagecreatefrom{$image_type}";

$src = $fun($path);

$newPath = generateUniqueFilePath('./img/');

ImageJpeg($src, $newPath, $quality);

return $newPath;

}

/**

* 生成唯一的文件路径

* @param $basePath string 文件夹路径

* @param $suffix string

* @return string 文件路径

*/

function generateUniqueFilePath($basePath, $suffix='.jpg') {

$filePath = $basePath . md5(microtime(true)) . $suffix;

return $filePath;

}

header('Content-Type:image/png');

$path = './img/jjj.jpg';

$imgPaths = imgResize($path, 10000, 10000);

// reQuality($path, 1);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值