图像大小调整功能返回黑色图像

时间:2014-06-10 15:55:29

标签: php image gd

我有一个采用窗口大小的函数,如果图像宽度小于窗口宽度,则显示原始图像,或者如果图像宽度大于windw,则调整图像大小以适应。

while($row = mysql_fetch_array($result)){

$file="../images/photos/".$row['file'];

//check the size of the source image
$x = getimagesize($file);            
$srcWidth  = $x['0'];
$srcHeight = $x['1'];

$ratio = $srcWidth/$srcHeight;

if($srcWidth < $width){
    //use original
    $img="<img src='".$file."' alt='original image' />";
}else{
    $height=round($srcHeight/$ratio);
    echo "Resize ".$file."\n";
    echo "Create new image ".$width." wide and ".$height." high"; 
    //$img="<img src='".$file."' style='width:".$width."px; height:auto' />";
    $img="<img src='image-resize.php?src=$file&width=$width&height=$height' alt='resized-image' />";
}

echo "<div class='photo'>";

if($row['dateAdded']<>""){
   echo "<p>Added on: ".date('m/d/Y', $row['dateAdded'])." by ".$row['username']."</p>";
}else{
   echo "<p>&nbsp;</p>";
}
if($row['title']<>""){
    echo "<h4>".htmlentities($row['title'],ENT_QUOTES)."</h4>";
}
echo $img;
echo "</div>";
}

这是调整大小的文件 - 我尽可能地删除它但仍然可以发现问题。图像肯定是jpg

<?php
// image source and extension
$src = $_GET[src];
$width = $_GET[width];
$height = $_GET[height];

//echo $imgSrc;

//get image path info
$ext_parts = pathinfo($src);

//get file extension type
//$ext=$ext_parts['extension'];
$ext="jpg";

    $img_base = imagecreatetruecolor($width, $height);

    //output the image directly to the browser
    header('Content-type: image/jpeg');
    imagejpeg($img_base, null, 100);

    /*
    if($ext == "jpg"){ 
        header('Content-type: image/jpeg');
        imagejpeg($img_base, null, 100);  
    }
    if($ext == "gif"){ 
        header('Content-type: image/gif');
        imagegif($img_base); 
    }
    if($ext == "png"){ 
        header('Content-type: image/png');
        imagepng($img_base, null, 9); 
    }
    */
    //clear memory
    imagedestroy($img_base);

?>

此链接显示问题 http://www.searchforsites.co.uk/mobile/images-test.php?id=1&width=500

任何指导都很受欢迎。

1 个答案:

答案 0 :(得分:0)

您必须使用imagecopyresized函数或imagecopyresampled来创建图像缩略图。您应该真正了解一下PHP文档 - 有完整的示例如何在PHP中创建这些函数的缩略图&#39;手册页。