本文实例讲述了php实现压缩图片尺寸并转为jpg格式的方法。分享给大家供大家参考,具体如下:
<?php
function imagetojpg($srcfile,$dstfile,$towidth,$toheight)
{
$quality=80;
$data = @getimagesize($srcfile);
switch ($data['2'])
{
case 1:
$im = imagecreatefromgif($srcfile);
break;
case 2:
$im = imagecreatefromjpeg($srcfile);
break;
case 3:
$im = imagecreatefrompng($srcfile);
break;
case 6:
$im = imagecreatefrombmp( $srcfile );
break;
}
// $dstx=$srcw=@imagesx($im);
// $dsty=$srch=@imagesy($im);
$srcw=@imagesx($im);
$srch=@imagesy($im);
//$towidth,$toheight
if($toheight/$srcw > $towidth/$srch){
$b = $toheight/$srch;
}else{
$b = $towidth/$srcw;
}
//计算出图片缩放后的宽高
// floor 舍去小数点部分,取整
$new_w = floor($srcw*$b);
$new_h = floor($srch*$b);
$dstx=$new_w;
$dsty=$new_h;
$ni=@imagecreatetruecolor($dstx,$dsty);
@imagecopyresampled($ni,$im,0,0,0,0,$dstx,$dsty,$srcw,$srch);
@imagejpeg($ni,$dstfile,$quality);
@imagedestroy($im);
@imagedestroy($ni);
}
//imagetojpg('源文件名','目标文件名',目标宽,目标高);
imagetojpg('test2.png','test2.jpg',80,50);
更多关于php相关内容感兴趣的读者可查看本站专题:《php图形与图片操作技巧汇总》、《php文件操作总结》、《php数组(array)操作技巧大全》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php面向对象程序设计入门教程》、《php网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!