显示透明 BLOB .png
好的,所以我在谷歌搜索和阅读评论后更改了我的代码。
但现在得到这个错误
警告:imagecopyresampled():提供的参数不是有效的图像资源 /home/realcas/public_html/eshop/ecms/system/classes/photofetch.php 上 第 51 行 –PNG IHDR –m 警告:imagepng() [function.imagepng]: gd-png:致命的 libpng 错误:zlib 错误 /home/realcas/public_html/eshop/ecms/system/classes/photofetch.php 上 第 54 行
警告:imagepng() [function.imagepng]:gd-png 错误:setjmp 返回 错误条件在 /home/realcas/public_html/eshop/ecms/system/classes/photofetch.php 上 第 54 行
警告:无法修改标头信息 - 标头已由 (输出开始于 /home/realcas/public_html/eshop/ecms/system/classes/photofetch.php:51) 在/home/realcas/public_html/eshop/ecms/system/classes/photofetch.php 第 55 行
这是新代码
<?
include_once('database.php');
//Fetch basic Profile
class fetchphoto extends Database{
public function countrysize($id){
$this->id = $id;
$array=preg_split('#(?<!\\\)\:#',$this->id);
if($array[1] == "9177156176671")
{
$max_width = 226;
$max_height = 3000;
}
$sth = mysql_query("SELECT categoryimage FROM shop_categories WHERE id = '".$array[0]."'");
while($r = mysql_fetch_assoc($sth)) {
$blobcontents = $r["categoryimage"];
$im = imagecreatefromstring($blobcontents);
$x = imagesx($im);
$y = imagesy($im);
$ratioh = $max_height/$y;
$ratiow = $max_width/$x;
$ratio = min($ratioh, $ratiow);
// New dimensions
$width = intval($ratio*$x);
$height = intval($ratio*$y);
// Temporarily increase the memory limit to allow for larger images
ini_set('memory_limit', '32M');
// create a new blank image
$newImage = imagecreatetruecolor($width, $height);
// Copy the old image to the new image
imagecopyresampled($newImage, $img, 0, 0, 0, 0, $width, $height, $w, $y);
// Output to a temp file
imagepng($newImage, null, 10);
header('Content-type: image/png');
return $newImage;
// Free memory
imagedestroy($newImage);
}
}
}
$fetchpicture = new fetchphoto();
$fetchpicture->Connect();
$fetchpicture->DB();
$fetchpicture->countrysize($_GET['pic']);
$fetchpicture->CloseDB();
?>
Ok, so I changed my code after a little googling and reading comments.
But now get this error
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in
/home/realcas/public_html/eshop/ecms/system/classes/photofetch.php on
line 51 ‰PNG IHDRâI¯©m Warning: imagepng() [function.imagepng]:
gd-png: fatal libpng error: zlib error in
/home/realcas/public_html/eshop/ecms/system/classes/photofetch.php on
line 54Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns
error condition in
/home/realcas/public_html/eshop/ecms/system/classes/photofetch.php on
line 54Warning: Cannot modify header information - headers already sent by
(output started at
/home/realcas/public_html/eshop/ecms/system/classes/photofetch.php:51)
in /home/realcas/public_html/eshop/ecms/system/classes/photofetch.php
on line 55
Here is the new code
<?
include_once('database.php');
//Fetch basic Profile
class fetchphoto extends Database{
public function countrysize($id){
$this->id = $id;
$array=preg_split('#(?<!\\\)\:#',$this->id);
if($array[1] == "9177156176671")
{
$max_width = 226;
$max_height = 3000;
}
$sth = mysql_query("SELECT categoryimage FROM shop_categories WHERE id = '".$array[0]."'");
while($r = mysql_fetch_assoc($sth)) {
$blobcontents = $r["categoryimage"];
$im = imagecreatefromstring($blobcontents);
$x = imagesx($im);
$y = imagesy($im);
$ratioh = $max_height/$y;
$ratiow = $max_width/$x;
$ratio = min($ratioh, $ratiow);
// New dimensions
$width = intval($ratio*$x);
$height = intval($ratio*$y);
// Temporarily increase the memory limit to allow for larger images
ini_set('memory_limit', '32M');
// create a new blank image
$newImage = imagecreatetruecolor($width, $height);
// Copy the old image to the new image
imagecopyresampled($newImage, $img, 0, 0, 0, 0, $width, $height, $w, $y);
// Output to a temp file
imagepng($newImage, null, 10);
header('Content-type: image/png');
return $newImage;
// Free memory
imagedestroy($newImage);
}
}
}
$fetchpicture = new fetchphoto();
$fetchpicture->Connect();
$fetchpicture->DB();
$fetchpicture->countrysize($_GET['pic']);
$fetchpicture->CloseDB();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不太清楚自己遇到的问题是什么。提供有关该问题的更多详细信息将会有所帮助。
话虽这么说,问题是您的 Content-Type 标头吗?在您的代码片段中,您传递的是
Content-Type: image/png
,而它应该是Content-Type: image/png< /代码>。
另外,看起来您在将图像发送给客户端之前正在销毁图像 - 尝试在调用
imagepng($im, null, 300)
之后移动imagedestory($im)
代码>.请务必检查您的返回值!
imagecopyresampled
可以在成功时返回true
,在失败时返回false
,这可以帮助您缩小失败的范围。编辑:正如 Korvin Szanto 所说,您还需要设置 imagealphablending 为 true。
对更新的响应
在您的新代码中注意到以下几点:
imagecreatefromstring
创建变量$im
,但稍后在调用$img
时使用$img
代码>图像复制重采样。imagecopyresampled
时,您使用了变量$w
,而它应该是$x
。imagecopyresampled
处发现问题。imagedestroy
。一旦你的函数返回,后面的任何事情都不会执行 - 所以你的图像永远不会被破坏。另外,您还需要销毁原始 PNG - 因此在 return 语句之前添加imagedestroy($im)
。You're not really clear on what the problems are that you are experiencing. Providing more detail on the problem will help.
That being said, is the problem your Content-Type header? In your code snippet you are passing
Content-Type: <span class="posthilit">image</span>/png
when it should beContent-Type: image/png
.Also, it looks like you are destroying the image before you send it out to the client - try moving
imagedestory($im)
after you callimagepng($im, null, 300)
.Be sure to check your return values too!
imagecopyresampled
can returntrue
on success andfalse
on failure which can help you narrow down the part that fails.EDIT: As Korvin Szanto said, you'll also want to set imagealphablending to true.
Response to update
A couple things noticed in your new code:
$im
fromimagecreatefromstring
but later you use$img
when callingimagecopyresampled
.imagecopyresampled
you're using the variable$w
when it should be$x
.imagecopyresampled
if you were checking them in the first place.imagedestroy
before the return. Once your function returns, nothing after it will execute - so your image will never be destroyed. Also, you will need to destroy the original PNG too - so add in aimagedestroy($im)
before your return statement.