如何使用 GD 对 PHP 结果图像进行着色

发布于 2024-10-20 21:36:00 字数 1203 浏览 5 评论 0原文

我有一个代码可以根据输入值调整图像大小和着色...问题是我只能用其他应用程序保存的新图像着色一次..请帮助我..我希望这里有很多 PHP 专家……

<?php
createImage(50,50, 0,0, 255);
function createImage($width, $height, $nR, $nG, $nB)
{
$image = imagecreatefrompng("source.png");
imagealphablending($image, false);  
imagesavealpha($image, true);       

      //resize the image
      $new_image = imagecreatetruecolor($width, $height);
      imagealphablending($new_image, false); 
      imagesavealpha($new_image, true);
      imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesx($image));

    //colorize the image
        $nrgb = str_pad(dechex($nR), 2, '0', STR_PAD_LEFT). str_pad(dechex($nG), 2, '0', STR_PAD_LEFT). str_pad(dechex($nB), 2, '0', STR_PAD_LEFT);              

       $newColor = $nrgb;

        $c2 = sscanf($newColor ,"%2x%2x%2x");

        for($i=0;$i<$width;$i++)
        {
            for($j=0;$j<$height;$j++)
            {
             $cIndex = imagecolorat($new_image,$i,$j);

             imagecolorset($new_image,$cIndex,$c2[0],$c2[1],$c2[2]);
            }
        }

        header("Content-Type: image/png");

        imagepng($new_image,"test.png");
}
?>

I have a code that resize and colorize the image accordingly input values... the problem is I can able to colorize only one time with fresh image saved by other application..Please help me.. I hope there are many PHP expers are here.....

<?php
createImage(50,50, 0,0, 255);
function createImage($width, $height, $nR, $nG, $nB)
{
$image = imagecreatefrompng("source.png");
imagealphablending($image, false);  
imagesavealpha($image, true);       

      //resize the image
      $new_image = imagecreatetruecolor($width, $height);
      imagealphablending($new_image, false); 
      imagesavealpha($new_image, true);
      imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesx($image));

    //colorize the image
        $nrgb = str_pad(dechex($nR), 2, '0', STR_PAD_LEFT). str_pad(dechex($nG), 2, '0', STR_PAD_LEFT). str_pad(dechex($nB), 2, '0', STR_PAD_LEFT);              

       $newColor = $nrgb;

        $c2 = sscanf($newColor ,"%2x%2x%2x");

        for($i=0;$i<$width;$i++)
        {
            for($j=0;$j<$height;$j++)
            {
             $cIndex = imagecolorat($new_image,$i,$j);

             imagecolorset($new_image,$cIndex,$c2[0],$c2[1],$c2[2]);
            }
        }

        header("Content-Type: image/png");

        imagepng($new_image,"test.png");
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

七月上 2024-10-27 21:36:00

在我看来,您正在操作图像资源并将其输出,然后想要返回并进一步操作它而不需要重新开始。您可以通过以下方式完成此操作:

a) 将图像资源保存为会话变量,然后在后续更改中使用该会话变量。

b) 在输出之前保存更改后的图像,然后打开保存的更改后的图像并从那里开始。我不知道您正在使用什么文件类型,但例如对于 gif 图像,您的代码应该使用 imagegif() 来输出图像。您还可以利用相同的函数(或其他图像类型的等效函数)来保存图像。

Sounds to me like you are manipulating an image resource and outputting it and then wanting to go back and further manipulate it without starting over. You can do this by

a) save the image resource as a session variable, and then use the session variable in subsequent alterations.

b) save the altered image before outputting it, and then open the saved altered image and go from there. I don't know what file type you are using but for instance with gif images your code should be using imagegif() to output the image. You would utilize this same function (or other image type equivalent function) to also save the image.

偏爱你一生 2024-10-27 21:36:00

我建议查看此处找到的 imagefilter 函数: http://php.net/manual/en /function.imagefilter.php

查看该页面上的 IMG_FILTER_COLORIZE

I suggest looking at the imagefilter function found here: http://php.net/manual/en/function.imagefilter.php

Look at IMG_FILTER_COLORIZE on that page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文