tags:

views:

429

answers:

4

i need to know which is better gd or imagemagic to resize an image

+3  A: 

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.

Pablo Santa Cruz
+1  A: 

"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.

Diomidis Spinellis
A: 

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

?>

Basiclife
A: 

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."