PHP + GD创建随机的黑色缩略图 [英] PHP+GD creating random black thumbnails

查看:60
本文介绍了PHP + GD创建随机的黑色缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此功能会创建一些随机的黑色图像,例如10%的时间, 数量不多,但是..您知道..不应该发生的事情.

This function is creating some random black images like.. 10% of the time, is not much, but.. you know.. shouldnt be happening.

class ImgResizer {
private $originalFile = '';
public function __construct($originalFile = '') {
    $this -> originalFile = $originalFile;
}
public function resize($newWidth, $targetFile) {
    if (empty($newWidth) || empty($targetFile)) {
        return false;
    }
    $src = imagecreatefromjpeg($this -> originalFile);
    list($width, $height) = getimagesize($this -> originalFile);
    $newHeight = ($height / $width) * $newWidth;

    if ($newHeight<'335') {
        //$newHeight='335';
    }
    $tmp = imagecreatetruecolor($newWidth, $newHeight);
    #$tmp = imagecreate($newWidth, $newHeight);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    if (file_exists($targetFile)) {
        unlink($targetFile);
    }
    imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for output image quality with 100 being the most luxurious
}

}

在error_log中没有错误.这是gd_info():

no errors given in error_log. Here is gd_info():

Array(
[GD Version] => bundled (2.0.34 compatible)
[FreeType Support] => 
[T1Lib Support] => 
[GIF Read Support] => 1
[GIF Create Support] => 1
[JPG Support] => 1
[PNG Support] => 1
[WBMP Support] => 1
[XPM Support] => 1
[XBM Support] => 1
[JIS-mapped Japanese Font Support] => )1

服务器是Linux.函数被这样调用: 假设$ imagen是实际的源图像,而$ imagendestino是新缩略图的路径和文件名.

server is linux. function is being called like this: assuming $imagen is the actual source image, and $imagendestino is the path and filename of the new thumbnail.

if (!file_exists($imagendestino)) {
        $work = new ImgResizer($imagen);
        $work -> resize(475, $imagendestino);
    }

提前谢谢!

推荐答案

您最有可能传递非JPEG图像.

Most likely your passing a non JPEG image.

可以将JPEG调整大小,但是由于该功能无法读取其他图像格式,因此会生成无效图像.结果是空白图像,即全零,这给出了黑色图像.由

A JPEG is re-sized fine, however as the function can't read a different image format, this produces an invalid image. The result is a blank image, i.e. all zeros, this gives a black image. created by

imagecreatetruecolor($newWidth, $newHeight);

当我为您运行类时,将其传递给PNG图像文件,它会提供以下警告并创建黑色图像:

when I've run you class passing it a PNG image file it gives these Warnings and creates a black image:

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'filename' is not a valid JPEG file
Warning: imagecopyresampled(): supplied argument is not a valid Image resource

很可能您已禁用警告,因此您不会收到这些消息.

most likely you have warning disable so you don't get these messages.

尝试使用

imagecreatefromstring(file_get_contents(filename))

代替

imagecreatefromjpeg(filename)

这样GD会根据您的文件头自动检测文件类型.

this way GD automatically detects the file type based on the file header for you.

这篇关于PHP + GD创建随机的黑色缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆