2011-02-02 56 views
2

我正在努力调整和重新采样使用PHP的一些jpeg图像。它需要任何大于500像素×500像素的图像,并使最大侧500像素。这应该是相对简单的,但每次运行脚本时,它都会产生黑色的jpeg。创建的jpeg具有适当的尺寸,但不包括调整大小的图像。 GD库已启用,我确定它正在查找原始图像。我一直在寻找这块代码一天半没有运气,我没有看到什么?imagecopyresampled使黑盒子,但不是重采样图像

<?php 
$testimage = 'SandyCayCaribbeanbeach.jpg'; 
$testfolder = "testimage/testimage.jpg"; 
list($orgwidth, $orgheight, $type, $attr) = getimagesize($testimage); 

echo "org. width " . $orgwidth . "px" . "<br />"; 
echo "org. height " . $orgheight . "px" . "<br />"; 

if($orgwidth > 500 || $orgheight > 500){ 
    if($orgwidth > $orgheight){ 
     header('Content-type: image/jpeg'); 
     $ratio = $orgwidth/500; 
     $newwidth = floor($orgwidth/$ratio); 
     $newheight = floor($orgheight/$ratio); 

     $image_p = imagecreatetruecolor($newwidth, $newheight); 
     $image = imagecreatefromjpeg($testimage); 
     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

     imagejpeg($image_p, $testfolder, 100); 
    } 
    else{ 
     header('Content-type: image/jpeg'); 
     $ratio = $orgheight/500; 
     $newheight = floor($orgheight/$ratio); 
     $newwidth = floor($orgwidth/$ratio); 

     $image_p = imagecreatetruecolor($newwidth, $newheight); 
     $image = imagecreatefromjpeg($testimage); 
     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

     imagejpeg($image_p, $testfolder, 100); 
    } 
} 
    ?> 

回答

2

首先确保您打开了错误报告。还要确保它可以找到源图像“SandyCayBaribbeanbeach.jpg”。

简单if(file_exists())在处理图像大小调整之前检查将有助于捕获错误。

+0

打开错误报告后,它显示在第20行我的高度和宽度变量未定义。他们是$宽度和$高度,但应该是$ orgwidth和$ orgheight。感谢错误报告提示,即时通讯新的和完全忘了它。 – Andalinmicphew 2011-02-02 17:12:27

0

仔细检查以确保您的源图像是真正的JPEG图像。如果您正在运行Windows,请在MS Paint中打开并重新保存为JPEG格式。这将有助于排除它作为不同格式的可能性。

0

我最近还在一段代码中争取了一段时间,发现如果未定义维度,imagecopyresampled甚至会返回1。确保设置了源高和宽度。

2

我发现,我不得不指定图像的完整路径一个URL即/path/to/image.jpg的

代替

http://www.blah.com/image.jpg

使其正常工作。希望它能帮助别人。