2

私はこれを思いつきました:

<?php 

$dir = $_GET['dir'];

header('Content-type: image/jpeg'); 

$create = imagecreatetruecolor(150, 150); 
$img = imagecreatefromjpeg($dir); 
imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, 150, 150); 

imagejpeg($create, null, 100); 

?>

以下にアクセスすることで機能します。

http://example.com/image.php?dir=thisistheimage.jpg

これは正常に動作します...しかし、出力はひどいものです:

代替テキスト

誰かが画像のコードを150 x 150で黒い領域を覆うように修正できますか...

ありがとう。

解決:

<?php 

$dir = $_GET['dir'];

header('Content-type: image/jpeg'); 

list($width, $height) = getimagesize($dir);

$create = imagecreatetruecolor(150, 150); 
$img = imagecreatefromjpeg($dir); 

$newwidth = 150;
$newheight = 150;

imagecopyresized($create, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($create, null, 100); 

?>
4

3 に答える 3

6

使用imagecopyresized:

$newwidth = 150;
$newheight = 150;
imagecopyresized($create, $image, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
于 2010-05-30T20:05:15.113 に答える
1

最後の 2150は、フル サイズの画像の元の幅と高さである必要があります。

于 2010-05-30T20:10:42.433 に答える
1

他の人が示唆したように、最後の 2 つのパラメーターは画像の元のサイズにする必要があります。

$dir がファイル名の場合、getimagesizeを使用して画像の元のサイズを取得できます。

imagecopyresized または imagecopyresampled を使用できます。違いは、imagecopyresized はコピーとサイズ変更を行いますが、imagecopyresampled は画像の再サンプリングも行うため、品質が向上します。

<?php 

$dir = $_GET['dir'];

header('Content-type: image/jpeg'); 

$create = imagecreatetruecolor(150, 150); 
$img = imagecreatefromjpeg($dir);
list($width, $height) = getimagesize($dir);
imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, $width, $height);

imagejpeg($create, null, 100); 

?>
于 2010-05-30T20:26:35.063 に答える