我是 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);
}