Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts

    image GD functions, truecolor and palette color count

    Just a quick question about the true color and palettes.
    Currently, I'm grabbing my image data from a WordML script, with no problems as of yet. One of the images that I have is named as a jpeg, though when converted to the gif format it appears that there are no (noticable) changes in the colors. This is not the case with every image. I've also noticed if I store this image in a gif format instead of a jpeg or png format that the size is significantly smaller. However, I do not want to do this gif format for everything, as it would result in many color losses. Is there anyway to count the number of colors used within the image to determine if it should be formated as a gif or png? I've found only one way to count the colors which are in use, but that function requires it to be a palette, which of course only has 256 colors, ruining the entire method of determining its format. What I do know for certain is that truecolor images will return 0 with the function. I'd say that I can call anything with a 0 to be a truecolor, but as mentioned this isn't the case with this image, for it looks the same in gif as in jpeg. BTW, this image only uses two unique colors, black and white, so I know off hand that it would look the same. Also, though getimagesize() would be great to use, I cannot get it to work without a file to go with it, fyi for you if you were going along that method, and I'm also hoping to do this without creating a temporary file if possible.
    Thanx in advance!

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure about the colour counting, but as for the getimagesize(), there is a way to get it to work without creating a file by using the url wrapper, simply create a little script that outputs your image data to the browser then just call that script as the url parameter in getimagesize. You can even do this from within the calling script if you make use of $_GET, something like this:

    PHP Code:
    // script.php
    if(isset($_GET['p'])) {
        
        
    header ("Content-type: image/png");
        
    $im = @imagecreatetruecolor(10075);
        
    $color imagecolorallocate($im25050100);
        
    imagestring($im51515,  "Test"$color);
        
    imagepng($im);
        
    imagedestroy($im);

    } else {

        
    $size getimagesize("http://www.domain.com/script.php?p=1");
        
    print_r($size);


    One thing to note is that I've found this returns false if you just reference the script without the full domain info, eg $size = getimagesize("script.php?p=1"); always fails for me, I don't know if I'm doing something wrong or if this is the correct behaviour (I actually get a "failed to open stream" error).

    Edit: Just a mad idea but maybe there's a way to get the colour count using imagecolorat() and working through the pixels one at a time, reading the colours into an array, then stripping out duplicate values from the array to arrive at a colour count?

    I've never tried this and another problem would be that imagecolorat doesn't work with url's (as far as I know) so you would definitely want to output the image to a temp file (plus it'd be pretty resource intensive to call the script as a url for every single pixel even in a 100 x 100 sized image, I guess). Oh well, it's something to think about as a last resort.
    Last edited by delinear; 06-20-2005 at 11:39 AM.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Hmm, currently I grab the image via image.php?name=xxx, for instance, and I couldn't get it to work with getimagesize. No worries, I thought the same as well that it should work, and personally, I think it should, but I may be missing something simple (like mime types or whatever).

    Interesting idea as well to count the pixels, and load as an array of known ones to count it. I could give that a shot. ATM I'm not worried of server load, so long as its handled decently, this would be a one time per image call, simply to determine if it should be saved using imagepng or imagegif.
    Does the imagecolorat and similar equiations use an image resource? I would think so... hmm, I'll give it a look over and take a look see, if theres a way, it will be found

    But in response the the actual question I was looking at, I take it there is currently no function in php to handle the truecolor count, correct?

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu
    Hmm, currently I grab the image via image.php?name=xxx, for instance, and I couldn't get it to work with getimagesize. No worries, I thought the same as well that it should work, and personally, I think it should, but I may be missing something simple (like mime types or whatever).
    Well, give it a try with "http://www.domain.com/image.php?name=xxx" as the argument in getimagesize, it should work. I can't see any reason why it won't work if you reference the script directly but it does if you reference it as a full url. I suppose it's no big job to dynamically grab the URL and put it in there but it's silly that you have to in the first place

    But in response the the actual question I was looking at, I take it there is currently no function in php to handle the truecolor count, correct?
    As far as I know, not without changing to palette first, which defies the object of you wanting to use it really.

    I'm assuming your thought was to use getimagesize to get the size of the original image then use something like imagetruecolortopalette and get the new image size and compare them to see if the latter is smaller, indicating a loss of colour?

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another thought is to transform the image to palette using imagetruecolortopalette() then use imagecolorstotal() and if the number of colours is 256 then you can probably assume the original had more than the maximum and you are suffering colour loss, since it would be a massive coincidence if the original image had exactly 256 colours, and since that would happen very infrequently you can probably live with not converting those rare images that did have 256 colours to begin with. If the number is < 256 then you know the original image has less than 256 colours and it's safe to convert.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •