douba8048 2010-06-11 22:27
浏览 37

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

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!

  • 写回答

1条回答 默认 最新

  • dongnuoyi8833 2011-07-07 02:42
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作