/** * desription 压缩图片 * @param sting $imgsrc 图片路径 * @param string $imgdst 压缩后保存路径 */ function image_png_size_add($imgsrc, $imgdst) { list($width, $height, $type) = getimagesize($imgsrc); $new_width = ($width > 600 ? 600 : $width) * 0.9; $new_height = ($height > 600 ? 600 : $height) * 0.9; 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); imagejpeg($image_wp, $imgdst, 75); imagedestroy($image_wp); } 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); imagejpeg($image_wp, $imgdst, 75); imagedestroy($image_wp); 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); imagejpeg($image_wp, $imgdst, 75); imagedestroy($image_wp); break; } }
function image_png_size_add($imgsrc, $limit_width) { list($width, $height, $type) = getimagesize($imgsrc); $percent = $limit_width / $width; //ͼƬѹËõ±È if ($width <= 220) { $percent = 1; } $new_width = $width * $percent; $new_height = $height * $percent; switch ($type) { case 1: header('Content-Type:image/gif'); $giftype = check_gifcartoon($imgsrc); if ($giftype) { $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromgif($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp); imagedestroy($image_wp); } else { $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromgif($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp); imagedestroy($image_wp); } 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); imagejpeg($image_wp); imagedestroy($image); imagedestroy($image_wp); 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); imagejpeg($image_wp); imagedestroy($image); imagedestroy($image_wp); break; case 4: $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); imagejpeg($image_wp); imagedestroy($image_wp); } break; } }
/** * desription 压缩图片 * @param sting $imgsrc 图片路径 * @param string $imgdst 压缩后保存路径 */ function image_png_size_add($imgsrc, $imgdst) { ////使用GD2 list($width, $height, $type) = getimagesize($imgsrc); if ($height > $width) { $scale = $width / $height; ////宽高比 $new_width = ($width > 600 ? 600 * $scale : $width) * 0.9; ///固定高度 $new_height = ($height > 600 ? 600 : $height) * 0.9; } else { if ($width >= $height) { $scale = $height / $width; ////宽高比 $new_width = ($width > 600 ? 600 : $width) * 0.9; ///固定宽度 $new_height = ($height > 600 ? 600 * $scale : $height) * 0.9; } } 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); imagejpeg($image_wp, $imgdst, 75); //unlink($imgsrc); imagedestroy($image_wp); imagedestroy($imgsrc); imagedestroy($image); unset($image); unset($image_wp); } 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); imagejpeg($image_wp, $imgdst, 75); //unlink($imgsrc); imagedestroy($image_wp); imagedestroy($imgsrc); imagedestroy($image); unset($image); unset($image_wp); 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); imagejpeg($image_wp, $imgdst, 75); //unlink($imgsrc); imagedestroy($image_wp); imagedestroy($imgsrc); imagedestroy($image); unset($image); unset($image_wp); break; } }