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

标签 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) {

/* 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"

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

最佳答案

这是因为您正在对 png 图像使用 imagecreatefromjpeg() 。您需要对这些图像使用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.

关于php - 用于创建缩略图的部分工作功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505303/

相关文章:

php - 使用 if 和 else 从数据库中查找并显示某些字符串

java swt 图像(图像数据)旋转

image - vue/cli重命名图片无法访问

javascript - 检查缩略图和图像是否存在

jquery - 如何删除和禁用Ninja Slider中的下一个,上一个按钮

php - 如何在php mysql中向手机号码发送群发短信

php - 如何将电子邮件本地部分截断为 'abc...@gmail.com'

php - 如何仅在 LIKE 查询 Laravel 中找到整个单词?

css - 多个边框,带填充,围绕图像

php - 我如何使用 PHP 获取网页的快照或缩略图?