png图片
function imageResizer($url, $width, $height) {
$image = imagecreatefrompng($url);
$width_orig = imagesx($image);
$height_orig = imagesy($image);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = floor($height*$ratio_orig);
} else {
$height = floor($width/$ratio_orig);
}
$image_p = @imagecreate($width, $height)or die("Cannot Initialize new GD image stream");
$alpha = imagecolorallocatealpha($image_p, 0, 0, 0, 127);
//为一幅图像分配颜色 + alpha
//int imagecolorallocatealpha ( resource $image , int $red , int $green , int $blue , int $alpha )
//imagecolorallocatealpha() 的行为和 imagecolorallocate() 相同,但多了一个额外的透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示完全透明。如果分配失败则返回 FALSE。
imagefill($image_p, 0, 0, $alpha);
$aa=imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagesavealpha($image_p, true);
header('Content-type: image/png');
imagepng($image_p,"2.png");//输出到图片文件2.png
}
imageResizer("1.png", 100, 20);
function imageResizer($url, $width, $height) {
list($width_orig, $height_orig) = getimagesize($url);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// 重新取样
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// 输出
header('Content-type: image/jpeg');
imagejpeg($image_p, "2.jpg", 100);
}
imageResizer("1.jpg", 100, 200);