如何使用 gd 库调整 bmp、tiff 图像的大小? 还提到 imagemagick 很好用

发布于 2024-08-01 23:10:41 字数 1542 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぺ禁宫浮华殁 2024-08-08 23:10:41

我更喜欢ImageMagick。 但我知道 GD 它也相当不错。

以下是如何使用 PHP 调整图像大小的示例:

   <?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;
   ?>

Here 是我从中获取示例的链接。 只需将其复制到问题中即可。 所有学分均归编写者所有。

I like ImageMagick better. But I know GD it's pretty good too.

Here's an example on how to resize an image using PHP:

   <?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;
   ?>

Here is the link where I got the example from. Just copied it to have it right in the question. All credits go to the person who wrote it.

绿光 2024-08-08 23:10:41

“更好”是一个主观术语。 许多调整大小算法可以提供更好的质量,但代价是更长的处理时间。 因此,决定您想要什么属性(良好的质量,或快速的响应时间),并查看每个库的结果。

"Better" is a subjective term. Many resizing algorithms can provide better quality at the expense of a higher processing time. So decide what attributes you want (good quality, or a fast response time), and look at the results of each library.

软糖 2024-08-08 23:10:41

只有有限数量的重采样算法可用于调整图像大小。 询问哪个程序更好意味着如果该程序实现了更好的算法,那么该程序被认为是“好的”。

There are only a limited number of resampling algorithms available to resize an image. Asking which program is better implies that if the program implements the better algorithms then the program is considered "good."

毁梦 2024-08-08 23:10:41

下面是我用 PHP 编写的缩略图。 我已经删除了添加阴影和边框的部分(不要认为我已经破坏了它,但还没有测试过)。
它使用了 PHP 中的 GD 库,我对结果一直很满意。

注意:您可能可以删除更多内容 - 例如,它设置缩略图的 BG 颜色,以便它与页面背景相匹配,等等...

在这种情况下,它将被这样调用:

thumbnail.php?size=400&image=SomeImage.jpg

唯一的小问题是大文件(即现代数码相机的高质量)它可能存在内存问题。 不过,我很少遇到这个问题 - 通常用户无法上传任何大小的内容,因为网络服务器不允许这样做。

<?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);

?>

Below is a thumbnailer I wrote in PHP. I've stripped out the bits that add drop shadows and borders (Don't think i've broken it but haven't tested).
This uses the GD library in PHP and I've always been happy with the results.

NB: You can probably strip out more - eg it sets the BG colour of the thumbnail so that it matches the page background, etc...

In this case, it would be called like this:

thumbnail.php?size=400&image=SomeImage.jpg

The only slight issue is that with large files (ie very high quality from modern digital cameras) it can have memory issues. I rarely hit this problem though - usually anything that size can't be uploaded by a user as the webserver won't allow it.

<?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);

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文