显示透明 BLOB .png

发布于 2024-12-12 01:39:35 字数 2551 浏览 0 评论 0原文

好的,所以我在谷歌搜索和阅读评论后更改了我的代码。

但现在得到这个错误

警告: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 54

Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns
error condition in
/home/realcas/public_html/eshop/ecms/system/classes/photofetch.php on
line 54

Warning: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何必那么矫情 2024-12-19 01:39:36

您不太清楚自己遇到的问题是什么。提供有关该问题的更多详细信息将会有所帮助。

话虽这么说,问题是您的 Content-Type 标头吗?在您的代码片段中,您传递的是 Content-Type: image/png ,而它应该是 Content-Type: image/png< /代码>。

另外,看起来您在将图像发送给客户端之前正在销毁图像 - 尝试在调用 imagepng($im, null, 300) 之后移动 imagedestory($im)代码>.

请务必检查您的返回值! imagecopyresampled 可以在成功时返回 true,在失败时返回 false,这可以帮助您缩小失败的范围。

编辑:正如 Korvin Szanto 所说,您还需要设置 imagealphablending 为 true。

对更新的响应

在您的新代码中注意到以下几点:

  1. 您从 imagecreatefromstring 创建变量 $im,但稍后在调用 $img 时使用 $img代码>图像复制重采样。
  2. 调用 imagecopyresampled 时,您使用了变量 $w,而它应该是 $x
  3. 请、请、开始检查您的返回值,而不是盲目地接受它们是否有效。如果您首先检查它们,您可能已经能够在 imagecopyresampled 处发现问题。
  4. 您需要在输出图像本身之前输出标头 - 否则您将收到“标头已发送”错误。
  5. 您需要在返回之前移动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 be Content-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 call imagepng($im, null, 300).

Be sure to check your return values too! imagecopyresampled can return true on success and false 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:

  1. You create the variable $im from imagecreatefromstring but later you use $img when calling imagecopyresampled.
  2. When calling imagecopyresampled you're using the variable $w when it should be $x.
  3. Please, please, please start checking your return values instead of blindly accepting that they work. You may have been able to catch the problem at imagecopyresampled if you were checking them in the first place.
  4. You need to output your header before you output the image itself - otherwise you'll get a "headers already sent" error.
  5. You need to move 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 a imagedestroy($im) before your return statement.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文