PHP+GD 创建随机黑色缩略图

发布于 2024-09-04 23:24:41 字数 1640 浏览 2 评论 0原文

这个函数正在创建一些随机的黑色图像,例如.. 10% 的时间, 不多,但是..你知道..不应该发生。

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():

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 是新缩略图的路径和文件名。

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

提前致谢!

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
}

}

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

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

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖彼岸 2024-09-11 23:24:41

您很可能传递的是非 JPEG 图像。

JPEG 的大小可以很好地调整,但是由于该函数无法读取不同的图像格式,因此会生成无效图像。结果是空白图像,即全零,这给出了黑色图像。当

imagecreatetruecolor($newWidth, $newHeight);

我运行你的类并传递给它一个 PNG 图像文件时,它会给出这些警告并创建一个黑色图像:

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

很可能你禁用了警告,所以你不会收到这些消息。

尝试使用

imagecreatefromstring(file_get_contents(filename))

而不是

imagecreatefromjpeg(filename)

这种方式,GD 会根据文件头自动为您检测文件类型。

Most likely your passing a non JPEG image.

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);

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.

try using

imagecreatefromstring(file_get_contents(filename))

instead of

imagecreatefromjpeg(filename)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文