<?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);
//www.veryhuo.com/a/view/36032.html
}
//ImageToJPG('源文件名','目标文件名',目标宽,目标高);
ImageToJPG('test2.png','test2.jpg',80,50);
php压缩图片尺寸,并转为jpg
最新推荐文章于 2024-09-25 01:48:02 发布