即时缩略图PHP

时间:2010-05-30 20:02:29

标签: php image-processing image-manipulation on-the-fly

我想出了这个:

<?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://domain.com/image.php?dir=thisistheimage.jpg

哪种方法很好......但输出很糟糕:

alt text http://i47.tinypic.com/119s47a.jpg

有人可以修复我的代码,使图像覆盖黑色区域为150 x 150 ......

感谢。

SOLUTION:

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

?>

3 个答案:

答案 0 :(得分:6)

使用imagecopyresized

$newwidth = 150;
$newheight = 150;
imagecopyresized($create, $image, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);

答案 1 :(得分:1)

最后2 150应该是完整尺寸图像的原始宽度和高度。

答案 2 :(得分:1)

正如其他人所说,最后两个参数应该是图像的原始大小。

如果$ 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); 

?>