简体   繁体   English

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

[英]image resize function returns black image

I have a function that takes the window size and if the image width is less than the window width shows the original image or if the image is wider than the windw resizes the image to fit. 我有一个采用窗口大小的函数,如果图像宽度小于窗口宽度则显示原始图像,或者如果图像宽度大于窗口宽度,则将图像调整为适合的大小。

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>";
}

This is the resize file - I have stripped it down as much as possible but still can spot the problem. 这是调整大小的文件-我已尽可能将其精简,但仍然可以发现问题。 The images are definitely jpg 图像肯定是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);

?>

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

Any guidance greatly received. 任何指导都收到很大。

You simple have to use imagecopyresized function or imagecopyresampled to create your image thumbnails. 您只需使用imagecopyresize函数或对imagecopyresampled即可创建图像缩略图。 You should really take a look at PHP documentation - there are complete examples how to create thumbnails in PHP on those functions' manual pages. 您应该真正看一下PHP文档-在这些函数的手册页上有完整的示例如何在PHP中创建缩略图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM