Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 生成缩略图-不需要的黑色背景_Php_Gd - Fatal编程技术网

Php 生成缩略图-不需要的黑色背景

Php 生成缩略图-不需要的黑色背景,php,gd,Php,Gd,我使用此脚本将图片从原始大小调整为缩略图: <?php function resize($newWidth, $originalFile, $targetFile) { $info = getimagesize($originalFile); $mime = $info['mime']; switch ($mime) { case 'image/jpeg': $image_create_func = 'imagecreatefromj

我使用此脚本将图片从原始大小调整为缩略图:

<?php

function resize($newWidth, $originalFile, $targetFile) {

$info = getimagesize($originalFile);

$mime = $info['mime'];

switch ($mime) {
        case 'image/jpeg':
                $image_create_func = 'imagecreatefromjpeg';
                $image_save_func = 'imagejpeg';
                $new_image_ext = 'jpg';
                break;

        case 'image/png':
                $image_create_func = 'imagecreatefrompng';
                $image_save_func = 'imagepng';
                $new_image_ext = 'png';
                break;

        case 'image/gif':
                $image_create_func = 'imagecreatefromgif';
                $image_save_func = 'imagegif';
                $new_image_ext = 'gif';
                break;

        default: 
                throw Exception('Unknown image type.');
}


$img = $image_create_func($targetFile,100);
$width = $info[0];
$height = $info[1];
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

if (file_exists($targetFile)) {
        unlink($targetFile);
}

$image_save_func($tmp, $targetFile);

echo "<img src='$targetFile'>";

}

?>

…但它会生成黑色背景的拇指。不会显示任何错误或警告。显示错误设置为“开”。我现在测试的所有图像都是.jpg格式。请问,我做错了什么?

将此添加到您的php文件中,然后重试:

ini\u集(“gd.jpeg\u忽略\u警告”,1)

我也有这个问题。gd库在某一点(文件编码)会自动失败。如果你让它忽略这些无声的错误,它应该可以工作

编辑:


好的,就像Rob Starling指出的那样,您尝试读取targetfile而不是原始文件。如果您更正了此问题,但仍然有黑色图像,请再次尝试我的解决方案:D

您从未实际读取来自
$originalFile

的图像数据。谢谢,我尝试过,但对我的情况无效。仍然是黑色背景:-(我还是不明白,请你再解释一下好吗?在哪里,哪一行,我必须读
$originalFile
?谢谢,你的意思是,我必须把
$img=$image\u create\u func($targetFile,100);
改成
$img=$image\u create\u func($originalFile,100);
?我试过了,但还是黑色背景。。。
<?php

echo "<pre>"; var_dump(gd_info()); echo "</pre>";

?>
array(12) {
["GD Version"]=>
string(26) "bundled (2.1.0 compatible)"
["FreeType Support"]=>
bool(true)
["FreeType Linkage"]=>
string(13) "with freetype"
["T1Lib Support"]=>
bool(true)
["GIF Read Support"]=>
bool(true)
["GIF Create Support"]=>
bool(true)
["JPEG Support"]=>
bool(true)
["PNG Support"]=>
bool(true)
["WBMP Support"]=>
bool(true)
["XPM Support"]=>
bool(true)
["XBM Support"]=>
bool(true)
["JIS-mapped Japanese Font Support"]=>
bool(false)
}