/** image Compression function * @param $orgin _file original picture file * @param $maxwidth maximum width * @param $maxheight Maximum height * @param $name Compressed picture name * @param $fil EType Picture Type */function resizeimage ($orgin _file, $maxwidth, $maxheight, $name, $filetype) {$im = "; switch ($filetype) {case '. jpg ': $im =imagecreatefromjpeg ($orgin _file); Break;case '. png ': $im =imagecreatefrompng ($orgin _file); Break;case '. GIF ': $im =imagecreatefromgif ($orgin _file); Break;default: $im =imagecreatefromgd ($orgin _file);} $pic _width = Imagesx ($im), $pic _height = Imagesy ($im), 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);//php system function imagecopyresampled ($newim, $im, 0,0,0,0, $newwidth, $newheight , $pic _width, $pic _height)//php system function}else{$newim = imagecreate ($newwidth, $newheight); imagecopyresized ($newim, $im, 0,0,0,0, $newwidth, $newheight, $pic _width, $pic _height);} $name = $name. $filetype, switch ($filetype) {case '. jpg ': imagejpeg ($newim, $name); Break;case '. png ': Imagepng ($newim, $ name); Break;case '. gif ': Imagegif ($newim, $name); Break;default:imagegd ($newim, $name);} Imagedestroy ($newim);} else{$name = $name. $filetype, switch ($filetype) {case '. jpg ': imagejpeg ($im, $name); Break;case '. png ': Imagepng ($im, $ name); Break;case '. gif ': Imagegif ($im, $name); Break;default:imagegd ($im, $name);}}Image Watermark can refer to the "Imagecopymerge" function, the manual is very detailed.
The above describes the PHP compressed picture function, including the content, I hope the PHP tutorial interested in a friend helpful.