Php 用于创建缩略图的部分工作函数
我有一个制作缩略图的部分工作功能,但是10%的图像并没有被创建为缩略图,而且它们是相同的10%。其余的90%有效。我不知道为什么。请看一下我的代码:Php 用于创建缩略图的部分工作函数,php,image,thumbnails,Php,Image,Thumbnails,我有一个制作缩略图的部分工作功能,但是10%的图像并没有被创建为缩略图,而且它们是相同的10%。其余的90%有效。我不知道为什么。请看一下我的代码: <?php $image = "511photo.jpg"; if ($image) { make_thumb("uploads", "thumbnails", $image, 500); } function make_thumb($imageFrom, $imageTo, $image, $thumbWidth) { /* rea
<?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)
?>
注意:是的,我确信它们都在uploads文件夹中-我已经仔细检查过了,现在我很困惑…检查文件类型,并在某个条件下使用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);
}
这是因为您正在为
png
图像使用imagecreatefromjpeg()。您需要对这些图像使用imagecreatefrompng()
$source_image = imagecreatefrompng($getFrom);
要检查图像类型,可以使用以下函数:
$imageType = exif_imagetype($getFrom);
if($imageType == IMAGETYPE_PNG) {
//It's PNG
} elseif($imageType == IMAGETYPE_JPEG) {
//It's JPEG
} //You can check more types here.
您正在使用imagejpeg函数进行png扩展,它将不支持请查看尝试查找如何上载png文件,但511photo.jpg是jpg,即使其他jpg工作,它也不工作如何使其支持所有图像类型?那么,为什么我能够从大多数jpg制作缩略图?511photo.jpg是jpg,但我无法从中生成缩略图。请在这里添加照片或给我们一个链接好吗?这是一个jpg文件类型,但当我添加png函数时,我突然可以从中生成缩略图。这没有任何意义。而且,一旦我使用imagecreatefrompng(),我就可以使用imagejpeg()从png创建。为什么?我看到的是,你上面提供的图像是PNG图像,有人刚刚将文件扩展名从PNG更改为jpg。在您添加了我的代码之后,它就开始工作了,因为exif\u imagetype()
会发现它是png格式的,即使它有jpg扩展名。
$imageType = exif_imagetype($getFrom);
if($imageType == IMAGETYPE_PNG) {
//It's PNG
} elseif($imageType == IMAGETYPE_JPEG) {
//It's JPEG
} //You can check more types here.