这里有新鲜出炉的 PHP 教程,程序狗速度看过来!
PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,入门门槛较低,易于学习,使用广泛,主要适用于 web 开发领域。PHP 的文件后缀名为 php。
这篇文章主要介绍了 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);
- }
- }
来源: http://www.phperz.com/article/17/0805/342209.html