如果图像尺寸超过50 kb,如何调整图像大小。
这是我目前的代码。
$sess=66666;
$images = $_FILES["photoimg"]["tmp_name"];
$new_images = "$sess.gif";
$width=200;
//*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
if($size[2] == 1) {
$images_orig = imagecreatefromgif($images);
} else if($size[2] == 2) {
$images_orig = imagecreatefromjpeg($images);
}else if($size[2] == 3) {
$images_orig = imagecreatefrompng($images);
}
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
Imagejpeg($images_fin,"images/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
答案 0 :(得分:4)
使用filesize()
。
$sess=66666;
$images = $_FILES["photoimg"]["tmp_name"];
$new_images = "$sess.gif";
$maxsize = 50 * 1024; // 50Kb
if( filesize($images) > $maxsize) {
$width=200;
//*** Fix Width & Heigh (Autu caculate) ***//
$size=getimagesize($images);
$height=round($width*$size[1]/$size[0]);
if($size[2] == 1) {
$images_orig = imagecreatefromgif($images);
} else if($size[2] == 2) {
$images_orig = imagecreatefromjpeg($images);
}else if($size[2] == 3) {
$images_orig = imagecreatefrompng($images);
}
$photoX = imagesx($images_orig);
$photoY = imagesy($images_orig);
$images_fin = imagecreatetruecolor($width, $height);
imagecopyresampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
imagejpeg($images_fin,"images/".$new_images);
imagedestroy($images_orig);
imagedestroy($images_fin);
} else {
imagejpeg($images,"images/".$new_images);
}
答案 1 :(得分:1)
测试文件大小如下:
$maxSize = 50 * 1024;
$sess=66666;
$images = $_FILES["photoimg"]["tmp_name"];
if(filesize($images) > $maxSize){
$sess=66666;
$images = $_FILES["photoimg"]["tmp_name"];
$new_images = "$sess.gif";
$width=200;
//*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
if($size[2] == 1) {
$images_orig = imagecreatefromgif($images);
} else if($size[2] == 2) {
$images_orig = imagecreatefromjpeg($images);
}else if($size[2] == 3) {
$images_orig = imagecreatefrompng($images);
}
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
Imagejpeg($images_fin,"images/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}