您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

PHP实现图片压缩的两则实例_PHP

2024/4/16 21:53:19发布5次查看
本文介绍了php实现图片压缩的两种方法,读者可以根据具体应用参考或加以改进,以适应自身应用需求!废话不多说,主要代码部分如下:
实例1:
600?600:$width)*0.9; $new_height =($height>600?600:$height)*0.9; switch($type){ case 1: $giftype=check_gifcartoon($imgsrc); if($giftype){ header('content-type:image/gif'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromgif($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); } break; case 2: header('content-type:image/jpeg'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); break; case 3: header('content-type:image/png'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); break; } } /** * desription 判断是否gif动画 * @param sting $image_file图片路径 * @return boolean t 是 f 否 */function check_gifcartoon($image_file){ $fp = fopen($image_file,'rb'); $image_head = fread($fp,1024); fclose($fp); return preg_match(/.chr(0x21).chr(0xff).chr(0x0b).'netscape2.0'./,$image_head)?false:true; } ?>
实例2:
$dw){ $par=$dw/$width; $width=$dw; $height=$height*$par; if($height>$dh){ $par=$dh/$height; $height=$dh; $width=$width*$par; } }elseif($height>$dh){ $par=$dh/$height; $height=$dh; $width=$width*$par; if($width>$dw){ $par=$dw/$width; $width=$dw; $height=$height*$par; } }else{ $width=$width; $height=$height; } $nimg = imagecreatetruecolor($width,$height); //新建一个真彩色画布 imagecopyresampled($nimg,$img,0,0,0,0,$width,$height,$w,$h);//重采样拷贝部分图像并调整大小 imagejpeg ($nimg,$image); //以jpeg格式将图像输出到浏览器或文件 return true; //如果是执行生成缩略图操作则 }else{ $w=imagesx($img); $h=imagesy($img); $width = $w; $height = $h; $nimg = imagecreatetruecolor($dw,$dh); if($h/$w>$dh/$dw){ //高比较大 $width=$dw; $height=$h*$dw/$w; $intnh=$height-$dh; imagecopyresampled($nimg, $img, 0, -$intnh/1.8, 0, 0, $dw, $height, $w, $h); }else{ //宽比较大 $height=$dh; $width=$w*$dh/$h; $intnw=$width-$dw; imagecopyresampled($nimg, $img, -$intnw/1.8, 0, 0, 0, $width, $dh, $w, $h); } imagejpeg ($nimg,$image); return true; }}?> 上传图片
允许上传的文件类型为:
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录