Jump to content

php gd using .gif palette on png image


patawic

Recommended Posts

http://wutwilluchoose.info/xbox/copy2.php

Their both in png format.

but for some reason the watermarked image is using a gif palette.

Heres the code that im curently using

 

 

<?php  
header('content-type: image/png');  
$watermark = imagecreatefrompng("http://avatar.xboxlive.com/avatar/I%20patawic%20I/avatar-body.png");  
imagesavealpha($watermark, false);
imagealphablending($watermark, false); 
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);  
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefrompng("base.png");  
$size = getimagesize("base.png");  
$dest_x = $size[0] - $watermark_width - 0;  
$dest_y = $size[1] - $watermark_height - 0;  
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, -120, $watermark_width, $watermark_height, 100);  
imagepng($image);  
imagedestroy($image);  
imagedestroy($watermark);  
?>

 

 

Link to comment
Share on other sites

it didnt work. but thats probably because ive done it wrong

Heres my code

<?php  
header('content-type: image/png');  
$watermark = imagecreatefrompng("http://avatar.xboxlive.com/avatar/patawic/avatar-body.png");  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark); 
$image = imagecreatetruecolor($watermark_width, $watermark_height);   
$image = imagecreatefrompng("base.png");  
$size = getimagesize("base.png");  
$dest_x = $size[0] - $watermark_width - 0;  
$dest_y = $size[1] - $watermark_height - 0;  
imagecopymerge_alpha($image, $watermark, $dest_x, $dest_y, 0, -120, $watermark_width, $watermark_height, 100);  
imagepng($image);  
imagedestroy($image);  
imagedestroy($watermark);  

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
$opacity=$pct;
// getting the watermark width
$w = imagesx($src_im);
// getting the watermark height
$h = imagesy($src_im);
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying that section of the background to the cut
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// inverting the opacity
$opacity = 100 - $opacity;
// placing the watermark now
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity);
} 

?>

Link to comment
Share on other sites

Due to the edit button misteriously disappearing  :wtf:

heres my current code

<?php  
header('content-type: image/png');  
$watermark = imagecreatefrompng("http://avatar.xboxlive.com/avatar/i%20patawic%20i/avatar-body.png");  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark); 
$image = imagecreatetruecolor($watermark_width, $watermark_height);   
$image = imagecreatefrompng("base.png");  
$size = getimagesize("base.png");  
$dest_x = $size[0] - $watermark_width - 0;  
$dest_y = $size[1] - $watermark_height - 0;  
imagecopymerge_alpha($image, $watermark, $dest_x, $dest_y, 0, -120, $watermark_width, $watermark_height, 100);  
imagepng($image);  
imagedestroy($image);  
imagedestroy($watermark);  

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
    if(!isset($pct)){ 
        return false; 
    } 
    $pct /= 100; 
    // Get image width and height 
    $w = imagesx( $src_im ); 
    $h = imagesy( $src_im ); 
    // Turn alpha blending off 
    imagealphablending( $src_im, false ); 
    // Find the most opaque pixel in the image (the one with the smallest alpha value) 
    $minalpha = 127; 
    for( $x = 0; $x < $w; $x++ ) 
    for( $y = 0; $y < $h; $y++ ){ 
        $alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF; 
        if( $alpha < $minalpha ){ 
            $minalpha = $alpha; 
        } 
    } 
    //loop through image pixels and modify alpha for each 
    for( $x = 0; $x < $w; $x++ ){ 
        for( $y = 0; $y < $h; $y++ ){ 
            //get current alpha value (represents the TANSPARENCY!) 
            $colorxy = imagecolorat( $src_im, $x, $y ); 
            $alpha = ( $colorxy >> 24 ) & 0xFF; 
            //calculate new alpha 
            if( $minalpha !== 127 ){ 
                $alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha ); 
            } else { 
                $alpha += 127 * $pct; 
            } 
            //get the color index with new alpha 
            $alphacolorxy = imagecolorallocatealpha( $src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha ); 
            //set pixel with the new color + opacity 
            if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){ 
                return false; 
            } 
        } 
    } 
    // The image copy 
    imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); 
} 

?>

 

but its still using a 256 color palette :S

Link to comment
Share on other sites

Think I got it.

<?php
header('content-type: image/png');

$insert = imagecreatefrompng("http://avatar.xboxlive.com/avatar/i%20patawic%20i/avatar-body.png");
$insert_width = imagesx($insert);
$insert_height = imagesy($insert);
$new_image = imagecreatetruecolor($insert_width, $insert_height);

imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127));
imagecopy($new_image, $insert, 0, 125, 0, 0, $insert_width, $insert_height);

imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagepng($new_image);

imagedestroy($new_image);
imagedestroy($insert);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.