Image Compression with PHP
- 2021-11-02 00:14:44
- OfStack
In this paper, we share the specific code of PHP to realize picture compression for your reference. The specific contents are as follows
/**
* Generate a picture
* @param string $im Source picture path
* @param string $dest Target picture path
* @param int $maxwidth Generate picture width
* @param int $maxheight Generate picture height
*/
function resizeImage($im, $dest, $maxwidth, $maxheight) {
$img = getimagesize($im);
switch ($img[2]) {
case 1:
$im = @imagecreatefromgif($im);
break;
case 2:
$im = @imagecreatefromjpeg($im);
break;
case 3:
$im = @imagecreatefrompng($im);
break;
}
$pic_width = imagesx($im);
$pic_height = imagesy($im);
$resizewidth_tag = false;
$resizeheight_tag = false;
if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
if ($maxwidth && $pic_width > $maxwidth) {
$widthratio = $maxwidth / $pic_width;
$resizewidth_tag = true;
}
if ($maxheight && $pic_height > $maxheight) {
$heightratio = $maxheight / $pic_height;
$resizeheight_tag = true;
}
if ($resizewidth_tag && $resizeheight_tag) {
if ($widthratio < $heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if ($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if ($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if (function_exists("imagecopyresampled")) {
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
} else {
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
}
imagejpeg($newim, $dest);
imagedestroy($newim);
} else {
imagejpeg($im, $dest);
}
}
/**
* Picture compression processing
* @param string $sFile Source picture path
* @param int $iWidth Custom picture width
* @param int $iHeight Custom picture height
* @return string Compressed picture path
*/
function getThumb($sFile,$iWidth,$iHeight){
// Picture common path
$public_path = '';
// Whether the picture exists or not is judged
if(!file_exists($public_path.$sFile)) return $sFile;
// Determine the picture format ( Picture file suffix )
$extend = explode("." , $sFile);
$attach_fileext = strtolower($extend[count($extend) - 1]);
if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
return '';
}
// Compressed picture file name
$sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
// Whether the picture has been compressed or not is judged, and if so, the compressed picture path is returned
if(file_exists($public_path.$sFileNameS)){
return $sFileNameS;
}
// Generate a compressed picture and store it in the same path as the original image
resizeImage($public_path.$sFile, $public_path.$sFileNameS, $iWidth, $iHeight);
if(!file_exists($public_path.$sFileNameS)){
return $sFile;
}
return $sFileNameS;
}
Use example:
// Original picture img/img.jpg
// Generate a compressed graph img/img_300_300.jpg
getThumb('img/img.jpg',300,300);