用于创建缩略图的部分工作功能

Partially working function for creating thumbnails

我有一个用于制作缩略图的部分工作功能,但 10% 的图像没有创建为缩略图,它们是完全相同的 10%。其他 90% 有效。我不确定为什么。请看看我的代码:

<?php

$image = "511photo.jpg";

if ($image) {
make_thumb("uploads", "thumbnails", $image, 500);
}

function make_thumb($imageFrom, $imageTo, $image, $thumbWidth) {

/* read the source image */
$getFrom = $imageFrom."/".$image;

$source_image = imagecreatefromjpeg($getFrom);
$width = imagesx($source_image);
$height = imagesy($source_image);

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

/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($thumbWidth, $thumbHeight);

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

/* create the physical thumbnail image to its destination */

$dest = $imageTo."/".$image;
imagejpeg($virtual_image, $dest);

} //end of function make_thumb($imageFrom, $imageTo, $image, $thumbWidth)

?>

注意:这里有一些其他的 $image 不起作用:

"434cute-anime-couple-drawing-on-tumblr.png"
"503anime_head_vectorized_by_cona_cru-d784ls0.png"

注意:是的,我确定它们都在上传文件夹中 - 我已经检查并仔细检查过,老实说,现在我很困惑...

检查文件类型并在条件下使用 imagejpeg 函数添加此代码作为示例添加您的变量和值

if($fileType=="image/png"){
   $im=ImageCreateFromPNG($add); 
   $width=ImageSx($im);              // Original picture width is stored
   $height=ImageSy($im);             // Original picture height is stored
   $newimage=imagecreatetruecolor($n_width,$n_height);                 
   imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
   ImagePng($newimage,$tsrc);
   chmod("$tsrc",0777);
}

这是因为您将 imagecreatefromjpeg() 用于 png 图片。您需要对这些图像使用 imagecreatefrompng()

$source_image = imagecreatefrompng($getFrom);

要检查图像类型,您可以使用 exif_imagetype() 函数:

$imageType =  exif_imagetype($getFrom);

if($imageType == IMAGETYPE_PNG) {
    //It's PNG
} elseif($imageType == IMAGETYPE_JPEG) {
    //It's JPEG
} //You can check more types here.