PHP 图像尺寸调整代码

yipeiwu_com5年前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实现的下载远程文件类定义与用法。分享给大家供大家参考,具体如下: <?php /** * 下载远程文件类支持断点续传 */ class Http...

深入理解PHP之数组(遍历顺序)  Laruence原创

深入理解PHP之数组(遍历顺序) Laruence原创

经常会有人问我, PHP的数组, 如果用foreach来访问, 遍历的顺序是固定的么? 以什么顺序遍历呢? 比如: 复制代码 代码如下: <?php $arr['laruence'...

php图片添加水印例子

图片添加水印我相信各位朋友都知道的,今天我们来看一段php的图片添加水印例子,希望文章能够帮助到各位朋友。 <?php /** * 图片添加水印...

php缩放图片(根据宽高的等比例缩放)实例介绍

推荐一个简单实用的缩放图片工具 SimpleImage,参考http://www.white-hat-web-design.co.uk/blog/resizing-images-with...

PHP防盗链的基本思想 防盗链的设置方法

盗链是指服务提供商自己不提供服务的内容,通过技术手段绕过其它有利益的最终用户界面(如广告),直接在自己的网站上向最终用户提供其它服务提供商的服务内容,骗取最终用户的浏览和点击率。受益者不...