开发者

how to colorize PHP result image using GD

开发者 https://www.devze.com 2023-02-15 09:08 出处:网络
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 th

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 th开发者_开发技巧e 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");
}
?>


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消