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函数代码

相关文章

phpmailer绑定邮箱的实现方法

phpmailer绑定邮箱的实现方法

本文实例讲述了phpmailer绑定邮箱的实现方法。分享给大家供大家参考,具体如下: 效果如下: 1.配置 <?php return array ( 'email...

给初学者的30条PHP最佳实践(荒野无灯)

1,和PHP手册成为好朋友 2,打开Error Reporting Error reporting 在 PHP 开发时是很有帮助的. 你可以在你代码中发现先前你没有发现的错误,因为并不是...

简介PHP的Yii框架中缓存的一些高级用法

页面缓存 页面缓存指的是在服务器端缓存整个页面的内容。随后当同一个页面被请求时,内容将从缓存中取出,而不是重新生成。 页面缓存由 yii\filters\PageCache 类提供支持,...

PHP分页效率终结版(推荐)

这里仅展示出设计思路,具体安全问题,是否同步更新等可根据具体需求添加修改:以前人们常用的PHP分页技术,通常都需要先统计出数据库中总信息条数才能判断出总页数并对其进行分页,,也就是说,每...

discuz authcode 经典php加密解密函数解析

原理如下,假如: 加密 明文:1010 1001 密匙:1110 0011 密文:0100 1010 得出密文0100 1010,解密之需和密匙异或下就可以了 解密 密文:0100 10...