Imagecolorallocatealpha(): Alpha component is out of range in

hello,

I am getting error in below function
how can i fix this

Warning: imagecolorallocatealpha(): Alpha component is out of range in

function boyutla($resim, $tresim, $max_en, $max_boy){  
              
    $arraylar = explode('.',$resim);
    $sonuc   = $arraylar[(count($arraylar) -1)];
    $sonuc = trim($sonuc);
    $sonuc = strtolower($sonuc);
    
    switch ($sonuc){
        case "jpeg": $islem=imagecreatefromjpeg($resim); break;
        case "JPEG": $islem=imagecreatefromjpeg($resim); break;
        case "jpg": $islem=imagecreatefromjpeg($resim); break;
        case "gif": $islem=imagecreatefromgif($resim); break;
        case "png": $islem=imagecreatefrompng($resim); break;
        case "JPG": $islem=imagecreatefromjpeg($resim); break;
        case "GIF": $islem=imagecreatefromgif($resim); break;
        case "PNG": $islem=imagecreatefrompng($resim); break;
    }
    
    $boyut = getimagesize($resim);  
    $en    = $boyut[0];
    $boy   = $boyut[1];
     
    $x_oran = $max_en  / $en;  
    $y_oran = $max_boy / $boy;  
     
            if (($en <= $max_en) and ($boy <= $max_boy)){  
                $son_en  = $en;  
                $son_boy = $boy;  
            }  
            else if (($x_oran * $boy) < $max_boy){  
                $son_en  = $max_en;  
                $son_boy = ceil($x_oran * $boy);  
            }  
            else {  
                $son_en  = ceil($y_oran * $en);  
                $son_boy = $max_boy;  
            }  
    
    $yeni = ImageCreateTrueColor($son_en,$son_boy);
    imagealphablending($yeni, false);
    imagesavealpha($yeni, true);
    $transparent = imagecolorallocatealpha($yeni, $son_en, $son_boy, $en, $boy);
    imagefilledrectangle($yeni, 0, 0, $son_en, $son_boy, $transparent);   
    imagecopyresampled($yeni,$islem,0,0,0,0,$son_en,$son_boy,$en,$boy);  
  
    switch ($sonuc){
        case "jpeg": imagejpeg($yeni,$tresim);  break;
        case "JPEG": imagejpeg($yeni,$tresim);  break;
        case "jpg": imagejpeg($yeni,$tresim);  break;
        case "gif": imagegif($yeni,$tresim); break;
        case "png": imagepng($yeni,$tresim); break;
        case "JPG": imagejpeg($yeni,$tresim);  break;
        case "GIF": imagegif($yeni,$tresim); break;
        case "PNG": imagepng($yeni,$tresim); break;            
    }
    return $resim; 
  }

Well, the layout for that function is:

 imagecolorallocatealpha(
    GdImage $image,
    int $red,
    int $green,
    int $blue,
    int $alpha
): int|false

And, the $alpha value MUST be INT between 0 to 127. I do not see any error-checking in your code to check for these limits. Normally, you would check if your $boy is within these limits. For example, you could add these lines just before the $transparent= line to limit the $boy range:

if ($boy<0) { $boy=0; }
if ($boy>127) { $boy=127; }

This would fix the error. But, you might want to check where you are creating the $boy and fix that section and then it would not be needed. You create $boy from getimagesize() function which does NOT return the same results for all image file types. getimagesize() is designed mostly to get the size of the image, not the alpha setting. The first index of this call is the width of the image, the second is the height. Therefore, you are setting $boy to the height of the image, not the ALPHA setting.

Also, only a few types of images use the ALPHA layer. Perhaps you should explain to us what you are attempting to do with this process. I personally do not understand what you are trying to process.

I use this for sizing with just a few constant values
For logo picture: 260x113
For profile picture: 80x80
For OG Picture: 600x315
I don’t know if “imagecolorallocatealpha” is required for these
If you say it is not necessary, we can remove the relevant section. what else to do when removing the line

Thank you

Well, Adem, for resizing, you do not need most any of your code. But, there are some things to mention.
GIF’s, Apng’s, Avif’s and some Jpeg’s can be animated. Resizing them can cause strange issues sometimes due to the different formats. For example you can not just “resize” any GIF. Does not work as you will loose your animation. You can do it, but, you need to use a GIF editor of some sort. ( There are hundreds of them. ) Also, most sites limit the types of images a user can upload. This would save a lot of code used in the resizing.

Now, you can just create a function to load any image and that will get the image into a variable and then just use the resize function to change to the correct size. Here is a StackOverflow that shows this kind of routine for the load section. Quite simple. Create Image Any File Type

But, let’s talk about PNG’s. They have a transparent background. But, this transparency was created by someone else. You need to keep that while resizing.

For a logo picture, profile picture or any picture that is uploaded by a user, I would suggest to only allow JPG’s. And, for your own smilies or emoticon’s, use your own and set the background correctly. If you need to allow all of those different image types, for some reason, you would have to write a different routine to resize each different image type. You can resize JPG’s with ease, but, GIF’s and PNG’s need a different resize process.

Not sure if this helps, but, let us know…

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service