【问题标题】:imagecreatefrompng makes imported image block-ish?imagecreatefrompng 使导入的图像块状?
【发布时间】:2011-10-05 19:05:53
【问题描述】:

我正在尝试使用以下 PHP 来将随机生成的文本文件应用于图像。 (我现在只是使用随机图像。)

<?php
header ("Content-type: image/png");

$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}


//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y,  $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>

但是,当我运行该代码时,导入的图像变得非常块状。 这是我正在测试的图像:
http://i.stack.imgur.com/LhNkv.png

这是它的实际显示方式:
http://i.stack.imgur.com/AAcHZ.png

我的研究表明,“imagecreate”会生成“调色板”图像 - 我认为这可能与我的错误有关,但我已经看到很多基本图像绝不失真的示例。

提前感谢您的见解。

(呃。它不会让我发布图片但让我上传它们就好了?)

更新
将代码更改为:

<?php
header ("Content-type: image/png");

$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}


//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
//$x = imagesx($im) - $width ;
//$y = imagesy($im) - $height;
//$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
//$textColor = imagecolorallocate ($im, 0, 0,0);
//imagestring ($im, $font, $x, $y,  $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>

产生与上面相同的块状效果,但现在也没有将文本写入图像(显然?)。

【问题讨论】:

  • 如果注释掉 $im = imagecreatefrompng("test.png"); 之间的所有内容,生成的图像会发生什么?和 imagepng($im); ?
  • 你刚才所做的就是从问题中去掉6行代码。您是否尝试过使用不同的图像?

标签: php imagecreatefrompng


【解决方案1】:

可能是 Alpha 混合问题。在保存图像之前尝试添加这些:

imagealphablending($im, true); 
imagesavealpha($im, true); 

【讨论】:

  • 有一个完全不同的问题,但这也解决了我的一些问题!荣誉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
相关资源
最近更新 更多