PHP+GD 创建随机黑色缩略图
这个函数正在创建一些随机的黑色图像,例如.. 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能传递的是非 JPEG 图像。
JPEG 的大小可以很好地调整,但是由于该函数无法读取不同的图像格式,因此会生成无效图像。结果是空白图像,即全零,这给出了黑色图像。当
我运行你的类并传递给它一个 PNG 图像文件时,它会给出这些警告并创建一个黑色图像:
很可能你禁用了警告,所以你不会收到这些消息。
尝试使用
而不是
这种方式,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
when I've run you class passing it a PNG image file it gives these Warnings and creates a black image:
most likely you have warning disable so you don't get these messages.
try using
instead of
this way GD automatically detects the file type based on the file header for you.