Php 如何使用gd库调整bmp、tiff图像的大小?还提到imagemagick,它很好用
我需要知道gd和imagemagick哪一个更适合调整图像大小我更喜欢。但我知道它也很不错 下面是一个关于如何使用PHP调整图像大小的示例:Php 如何使用gd库调整bmp、tiff图像的大小?还提到imagemagick,它很好用,php,php4,Php,Php4,我需要知道gd和imagemagick哪一个更适合调整图像大小我更喜欢。但我知道它也很不错 下面是一个关于如何使用PHP调整图像大小的示例: <?php if(!extension_loaded('imagick')) { dl('imagick.so'); } $img = strip_tags($_GET['imagename']); if(isset($_GET['size'])) { $siz
<?php
if(!extension_loaded('imagick')) {
dl('imagick.so');
}
$img = strip_tags($_GET['imagename']);
if(isset($_GET['size'])) {
$size = strip_tags($_GET['size']);
} else {
$size = 0;
}
if(isset($_GET['vsize'])) {
$vsize = strip_tags($_GET['vsize']);
} else {
$vsize = 0;
}
$image = new Imagick($img);
$image->thumbnailImage($size, $vsize);
header("Content-type: image/png");
print $image;
?>
是我从中获得示例的链接。只是把它抄下来,让它在问题上正确。所有的学分都归作者所有。“更好”是一个主观术语。许多调整大小的算法可以以牺牲更高的处理时间为代价提供更好的质量。因此,请确定您想要的属性(高质量或快速响应时间),并查看每个库的结果。下面是我用PHP编写的缩略图。我已经去掉了添加阴影和边框的部分(不要认为我已经破坏了它,但还没有测试)。 这使用了PHP中的GD库,我对结果一直很满意 注意:你可能可以去掉更多的内容-例如它设置缩略图的背景颜色,使其与页面背景相匹配,等等 在这种情况下,可以这样称呼:
thumbnail.php?size=400&image=SomeImage.jpg
唯一轻微的问题是,对于大文件(即现代数码相机的高质量文件),它可能存在内存问题。但我很少碰到这个问题——通常,任何大小的东西都不能被用户上传,因为Web服务器不允许
<?php
$defaultsize = 400;
$defaultimage = "images/error.jpg";
ini_set("memory_limit", "32M");
$red = isset($_REQUEST['r']) ? $_REQUEST['r'] : 255;
$green = isset($_REQUEST['g']) ? $_REQUEST['g'] : 255;
$blue = isset($_REQUEST['b']) ? $_REQUEST['b'] : 255;
if(!isset($_REQUEST['size'])) {
$maxWidth=$defaultsize;
$maxHeight=$defaultsize;
} else {
$maxWidth=$_REQUEST['size'];
$maxHeight=$_REQUEST['size'];
}
if(!isset($_REQUEST['image'])) {
$picurl = $defaultimage;
} else {
$picurl = "../" . stripslashes($_REQUEST['image']);
}
//Find out about source file
$srcDetails = @getimagesize($picurl);
if($srcDetails) {
$srcWidth=$srcDetails[0];
$srcHeight=$srcDetails[1];
} else {
$srcWidth=$maxWidth;
$srcHeight=$maxHeight;
}
if($srcWidth/$srcHeight < $maxWidth/$maxHeight) {
//Too wide
$width = $maxHeight / $srcHeight * $srcWidth;
$height = $maxHeight / $srcHeight * $srcHeight;
} else {
//Too tall
$width = $maxWidth / $srcWidth * $srcWidth;
$height = $maxWidth / $srcWidth * $srcHeight;
}
switch ($srcDetails[2]) {
case 1: //GIF
$srcImage = ImagecreateFromGIF($picurl);
break;
case 2: //JPEG
$srcImage = ImagecreateFromJPEG($picurl);
break;
case 3: //PNG
$srcImage = ImagecreateFromPNG($picurl);
break;
case 6: //WBMP
$srcImage = ImagecreateFromWBMP($picurl);
break;
default:
//Possibly add some "Unknown File Type" error code here. However, if we do't return an image, we will error nicely later anyway
break;
}
if(@!$srcImage) {
// The nice error for no source image (include error mail to yourself here if you want...)
$srcImage = imagecreate($maxWidth, $maxHeight); /* Create a blank image */
$bgc = imagecolorallocate($srcImage, 255, 255, 255);
$tc = imagecolorallocate($srcImage, 0, 0, 0);
imagefilledrectangle($srcImage, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
imagestring($srcImage, 4, 5, 5, "Error resizing image", $tc);
imagestring($srcImage, 4, 5, 20, "Tech support department", $tc);
imagestring($srcImage, 4, 5, 35, "has been informed", $tc);
}
//Create thumbnail
$thumb = imagecreatetruecolor ($width, $height);
$bg = ImageColorAllocate($thumb, $red, $green, $blue);
imagefill ($thumb, 0, 0, $bg);
//Add the image itself
Imagecopyresized ($thumb, $srcImage, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
//Add a black border
imageline($thumb, 0, 0, 0, $height, $black);
imageline($thumb, 0, 0, $width, 0, $black);
imageline($thumb, 0, $height, $width, $height, $black);
imageline($thumb, $width, $height, $width, 0, $black);
//output header
//I leave this so late so if there ARE any errors, they are displayed as text not a broken image
//(this will happen when looking at the thumnailer directly but will display as a broken image in a webpage still)
header("Content-type: image/PNG");
imagePNG($thumb);
//Clear up memory
imagedestroy($srcImage);
?>
可用于调整图像大小的重采样算法数量有限。询问哪个程序更好意味着如果该程序实现了更好的算法,那么该程序被认为是“好的”。您介意详细说明您的要求吗?这可能会帮助人们引导你选择哪一个对你来说更好,但如果没有上下文,“哪一个更好”只是一个意见问题。