PHP 实现等比压缩图片尺寸和大小实例代码

yipeiwu_com6年前PHP代码库

废话不多说了,直接给大家贴php等比压缩图片大小的相关代码了,具体代码如下所示:

<?php
$im = imagecreatefromjpeg('D:phpplace.jpeg');
resizeImage($im,,,'xinde','.jpg');
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);
echo "start-----------------" ;
if(($maxwidth && $pic_width > $maxwidth) && ($maxheight && $pic_height > $maxheight))
{
if($maxwidth && $pic_width>$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}
if($maxheight && $pic_height>$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}
if($resizewidth_tag && $resizeheight_tag)
{
if($widthratio<$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if(function_exists("imagecopyresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

修改了一个很不错的php验证码(支持中文)

php英文验证码captcha.class.php 复制代码 代码如下: <?php class Captcha { private $width; private $height...

详细对比php中类继承和接口继承

PHP类继承: 1、PHP类不支持多继承,也就是子类只能继承一个父类,但是支持多层次继承 比如: class frist{ public function __construct...

PHP CLI模式下的多进程应用分析

PHP在很多时候不适合做常驻的SHELL进程, 他没有专门的gc例程, 也没有有效的内存管理途径. 所以如果用PHP做常驻SHELL, 你会经常被内存耗尽导致abort而unhappy....

Fedora下安装php Redis扩展笔记

一、安装编译工具 复制代码 代码如下: yum install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-d...

PHP利用REFERER根居访问来地址进行页面跳转

比如,我有一个开发一个黄页源码上转到了【宜配屋www.yipeiwu.com】。之前定了一个演示程序地址: //www.jb51.net 而现在这个域名需要用来作其它的站,又不杀望原来的...