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 Smarty date_format [格式化时间日期]

Example 5-8. date_format[日期格式] index.php: 复制代码 代码如下: $smarty = new Smarty; $smarty->assign...

探讨:parse url解析URL,返回其组成部分

parse_url(PHP 4, PHP 5)parse_url — 解析 URL,返回其组成部分说明array parse_url ( string $url )本函数解析一个 URL...

限制ckeditor上传图片文件大小的方法

一种可以通过修改PHP.INI配置文件上传大小来限制,另一种方法只能手动修改Fckeditor源码,方法如下打开editor/filemanager/connectors/php目录下c...

php中替换字符串中的空格为逗号','的方法

今天在网查到一篇介绍php中替换字符串中的空格为逗号','的文章,作个日记保存下来。 复制代码 代码如下: <pre name="code" class="php"><...

php字符串截取的简单方法

复制代码 代码如下:strpos(string,find,start)实例:复制代码 代码如下:<?php  echo strpos("Hello world!","wo...