php Realization of Equal Scale Compression Picture
- 2021-10-27 06:41:44
- OfStack
In this paper, we share the specific code of php to compress pictures in equal proportion for your reference. The specific contents are as follows
/**
* desription Compressed picture
* @param sting $imgsrc Picture path
* @param string $imgdst Save the path after compression
*/
public function compressedImage($imgsrc, $imgdst) {
list($width, $height, $type) = getimagesize($imgsrc);
$new_width = $width;// Width of compressed picture
$new_height = $height;// The compressed picture is high
if($width >= 600){
$per = 600 / $width;// Calculate the proportion
$new_width = $width * $per;
$new_height = $height * $per;
}
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);
//90 It represents the quality and compressed picture capacity
imagejpeg($image_wp, $imgdst, 90);
imagedestroy($image_wp);
imagedestroy($image);
}
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);
//90 It represents the quality and compressed picture capacity
imagejpeg($image_wp, $imgdst, 90);
imagedestroy($image_wp);
imagedestroy($image);
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);
//90 It represents the quality and compressed picture capacity
imagejpeg($image_wp, $imgdst, 90);
imagedestroy($image_wp);
imagedestroy($image);
break;
}
}