PHP 图像尺寸调整代码

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];
if($ext == "jpg" || $ext == "jpeg")
$im = imagecreatefromjpeg($tmpname);
elseif($ext == "png")
$im = imagecreatefrompng($tmpname);
elseif($ext == "gif")
$im = imagecreatefromgif($tmpname);
$x = imagesx($im);
$y = imagesy($im);
if($x <= $xmax && $y <= $ymax)
return $im;
if($x >= $y) {
$newx = $xmax;
$newy = $newx * $y / $x;
}
else {
$newy = $ymax;
$newx = $x / $y * $newy;
}
$im2 = imagecreatetruecolor($newx, $newy);
imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
return $im2;
}

这里是摘自【宜配屋www.yipeiwu.com】之前发布的文章。更多的技巧可以参考。
收集的二十一个实用便利的PHP函数代码

相关文章

php实现指定字符串中查找子字符串的方法

本文实例讲述了php实现指定字符串中查找子字符串的方法。分享给大家供大家参考。具体分析如下: 对strpos()函数可以用来在php中查找子字符串。strpos()函数将试图找到子字符串...

PHP添加PNG图片背景透明水印操作类定义与用法示例

本文实例讲述了PHP添加PNG图片背景透明水印操作类定义与用法。分享给大家供大家参考,具体如下: 图片相关操作类 class ImageTool { private $image...

PHP实现的多进程控制demo示例

本文实例讲述了PHP实现的多进程控制。分享给大家供大家参考,具体如下: 自己写了个多进程控制的框架代码,留着备查 declare(ticks=1); function sigHand...

PHP中用hash实现的数组

PHP中使用最多的非Array莫属了,那Array是如何实现的?在PHP内部Array通过一个hashtable来实现,其中使用链接法解决hash冲突的问题,这样最坏情况下,查找Arra...

PHP排序算法之基数排序(Radix Sort)实例详解

本文实例讲述了PHP排序算法之基数排序(Radix Sort)。分享给大家供大家参考,具体如下: 基数排序在《大话数据结构》中并未讲到,但是为了凑齐八大排序算法,我自己通过网络学习了这个...