1

我是 php 新手,我正在尝试制作缩略图

$src 是图像的路径 $thumbWidth 是所需的宽度 $imageName 并不重要,它需要传递给生成缩略图的 html 代码的函数。

问题出在第 174 行,如果图像是 jpeg 文件,函数返回 false,然后 $source_image 为 false,我将其注释掉,谁能解释为什么?

这是我的方法:

 function makeThumb( $src, $thumbWidth, $imageName )
 {
$count = 0;
$len = strlen($src);
$indexlen = $len - 1;
$sourceArray = str_split($src);
for($i = $indexlen; $i > -1; $i--)
{
    if($sourceArray[$i] == '.')
    {
        $count = $count + 1;
        if($count == 1)
        {
            $hold = $i; 
        }
    }
}

$ending = substr($src, $hold, $len);

if($ending === '.gif') 
{
    $type = '.gif';
    $source_image = imagecreatefromgif($src);
}
if($ending === '.jpeg' || $ending === '.pjpeg' || $ending === '.jpg') 
{
    $type = '.jpg';
    $source_image = imagecreatefromjpeg($src);
}
if($ending === '.png')
{
    $type = '.png';
    $source_image = imagecreatefrompng($src);
}
else
{   
    //throw new Exception('This file is not in JPG, GIF, or PNG format!');
    $type = null;
}

/* read the source image */
if($ending = null)
{   return null;    } 

$width = imagesx($src);
$height = imagesy($src);

$newWidth = $thumbWidth;

/* find the "desired height" of this thumbnail, relative to the desired width  */
$newHeight = floor($height * ($newWidth / $width));

/* create a new, "virtual" image */
$tempImg = imagecreatetruecolor($desired_width, $desired_height);

$pic = formatImage($tempImg, $imageName);

return $pic;

/* copy source image at a resized size */
//imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

/* create the physical thumbnail image to its destination */
//imagejpeg($virtual_image, $dest);

}

4

1 回答 1

2

Are you sure that the image is a valid jpeg image?

You check the type of the image by extension. You can check get the extension in a much simpler way. You should also check if the file exists:

function makeThumb( $src, $thumbWidth, $imageName )
{
    if(!file_exists($src))
        throw new Exception('The file '.$src.' does not exist.');

    $ext = pathinfo($src, PATHINFO_EXTENSION);

But checking the type of an image using extension is not reliable. There is another way to check that using the getimagesize function:

function makeThumb( $src, $thumbWidth, $imageName )
{
    if(!file_exists($src)) 
        throw new Exception('The file '.$src.' does not exist.');


    $info = getimagesize($src);

    if($info === false)
        throw new Exception('This file is not a valid image');

    $type    = $info[2];
    $width   = $info[0]; // you don't need to use the imagesx and imagesy functions
    $height  = $info[1];

    switch($type) {
        case IMAGETYPE_JPEG:
            $source_image = imagecreatefromjpeg($src);
            break;
        case IMAGETYPE_GIF:
            $source_image = imagecreatefromgif($src);
            break;
        case IMAGETYPE_PNG:
            $source_image = imagecreatefrompng($src);
            break;
        default:
            throw new Exception('This file is not in JPG, GIF, or PNG format!');
    }

    $newWidth = $thumbWidth;
    // ..and here goes the rest of your code

Please note - I did not check the rest of your code since your problem is related to the first part of the function.

于 2013-06-30T16:39:45.287 回答