0

画像のサイズを変更するには、gd と imagemagick のどちらが優れているかを知る必要があります

4

4 に答える 4

4

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

これは、例を取得したリンクです。それをコピーして、質問に正しく記入してください。すべてのクレジットは、それを書いた人に帰属します。

于 2009-08-16T12:15:36.387 に答える
2

「より良い」は主観的な用語です。多くのサイズ変更アルゴリズムは、処理時間が長くなりますが、より良い品質を提供できます。したがって、必要な属性 (高品質または高速応答時間) を決定し、各ライブラリの結果を確認します。

于 2009-08-16T12:18:30.880 に答える
1

以下は私がPHPで書いたサムネラーです。ドロップ シャドウと境界線を追加するビットを取り除きました (壊れたとは思いませんが、テストはしていません)。これは PHP の GD ライブラリを使用しており、私は常に結果に満足しています。

NB: おそらくもっと削除できます。たとえば、ページの背景と一致するようにサムネイルの BG 色を設定するなど...

この場合、次のように呼び出されます。

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

?>
于 2009-08-16T13:22:45.300 に答える
1

画像のサイズ変更に使用できるリサンプリングアルゴリズムの数は限られています。どちらのプログラムが優れているかという質問は、そのプログラムがより優れたアルゴリズムを実装している場合、そのプログラムは「優れている」と見なされることを意味します。

于 2009-08-16T14:02:41.540 に答える