【问题标题】:Show transparent BLOB .png显示透明 BLOB .png
【发布时间】:2011-10-27 04:11:26
【问题描述】:

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

但是现在得到这个错误

警告:imagecopyresampled():提供的参数不是有效的图像资源 /home/realcas/public_html/eshop/ecms/system/classes/photofetch.php 上 第 51 行 ‰PNG IHDRâI¯©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();
?>

【问题讨论】:

    标签: php image blob


    【解决方案1】:

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

    话虽如此,问题是您的 Content-Type 标头吗?在您的代码 sn-p 中,您传递的是 Content-Type: &lt;span class="posthilit"&gt;image&lt;/span&gt;/png,而它应该是 Content-Type: image/png

    此外,您似乎在将图像发送给客户端之前将其销毁 - 在您调用 imagepng($im, null, 300) 后尝试移动 imagedestory($im)

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

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

    响应更新

    在您的新代码中注意到几件事:

    1. 您从imagecreatefromstring 创建变量$im,但后来在调用imagecopyresampled 时使用$img
    2. 调用imagecopyresampled 时,您使用的是变量$w,而它应该是$x
    3. 请,请,开始检查您的返回值,而不是盲目地接受它们有效。如果您首先检查它们,您可能已经能够通过imagecopyresampled 发现问题。
    4. 您需要在输出图像本身之前输出您的标头 - 否则您会收到“标头已发送”错误。
    5. 您需要在返回之前移动imagedestroy。一旦你的函数返回,它就不会执行任何东西——所以你的图像永远不会被破坏。此外,您还需要销毁原始 PNG - 因此在您的退货声明之前添加 imagedestroy($im)

    【讨论】:

    • 他还想用imagealphblending($im,true);imagealphablending设置为true
    • 对不起,一旦我做出这些改变,它甚至没有加载图像
    • 说真的,在将代码发布到网站之前检查您的变量。
    猜你喜欢
    • 2011-06-22
    • 2012-03-25
    • 2014-12-18
    • 2019-12-12
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2014-09-30
    • 2018-07-18
    相关资源
    最近更新 更多