White Marcus
White Marcus

Reputation: 305

Image transparency in imagepng() and imagegif()

I want to resize 2 types of images (png, gif) with transparent background...

  1. But in png it works well in big size of the original image. eg. Original file dimension is 150x150 enter image description here

    change into 200x200 or exceeding the original size it works well. but in 100x100 it displays enter image description here

  2. Then the last GIF it doesn't work.

    Here my code for png

    $new_img = imagecreatetruecolor($max_w, $max_h);

    imagealphablending($new_img, false);
    imagesavealpha($new_img, true);

    $transparentindex = imagecolorallocatealpha($new_img, 255, 255, 255, 127);

    imagefill($new_img, 0, 0, $transparentindex);

    imagepng($new_img, $dir);

    Here my code for gif

    imagegif($new_img, $dir);

Upvotes: 2

Views: 783

Answers (2)

Mihai Iorga
Mihai Iorga

Reputation: 39724

Here is a little hack :) Don't have any other ideas.

It involves a second image (1x1 trasparent gif)

  1. create image
  2. resize
  3. create a transparent background with same size as resized image
  4. use imagecolortransparent() to get the rgb index
  5. imagecopymerge() images
  6. voilà

.

$dir = 'https://i.sstatic.net/DbhrJ.png';
$max_w = 50;
$max_h = 50;

// get image size
list($src_w, $src_h) = getimagesize($dir);

if ($src_w > $max_w) {
    $ratio = $max_w / $src_w;
    $max_w = $src_w * $ratio;
}
else if ($src_h > $max_h) {
    $ratio = $max_h / $src_h;
    $max_h = $src_h * $ratio;
}

// resize image to $max_w and $max_h, and also save alpha
$src = imagecreatefromstring(file_get_contents($dir));
$new_img = imagecreatetruecolor($max_w, $max_h);
imagealphablending($new_img, false);
imagesavealpha($new_img, true);
imagecopyresampled($new_img, $src, 0, 0, 0, 0, $max_w, $max_h, $src_w, $src_h);

// create a new image with $max_w and $max_h
$maxsize = imagecreatetruecolor($max_w, $max_h);

// add 1x1 gif and resize, then copy over $maxsize
$background = imagecreatefromgif("http://i.imgur.com/atRjCdk.gif"); // transparent 1x1 gif
imagecopyresampled($maxsize, $background, 0, 0, 0, 0, $max_w, $max_h, 1, 1);

// allocate transparency
$transparent = imagecolortransparent($maxsize);
$transparent_index = imagecolorallocate($maxsize, $transparent['red'], $transparent['green'], $transparent['blue']);
imagecolortransparent($maxsize, $transparent_index);

// image copy
imagecopymerge($maxsize, $new_img, 0, 0, 0, 0, $max_w, $max_h, 100);

// save or output
imagegif($maxsize, $path_to_save);

// destroy images
imagedestroy($maxsize);
imagedestroy($new_img);
imagedestroy($background);
imagedestroy($src);

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 207678

I know no PHP, but this seems to do the trick:

#!/usr/local/bin/php -f
<?php
    $neww=100;
    $newh=100;

    // Load original image and get its dimensions
    $img = imagecreatefrompng("badge.png");
    $w=imagesx($img);
    $h=imagesy($img);

    // Create output image, and fill with lots of transparency
    $out = imagecreatetruecolor($neww,$newh);
    imagealphablending($out,false);
    $transparentindex = imagecolorallocatealpha($out,0,0,0,127);
    imagefill($out,0,0,$transparentindex);
    imagesavealpha($out, true);

    // Copy original image, reized on top
    imagecopyresized($out,$img,0,0,0,0,$neww,$newh,$w,$h);
    imagepng($out,"out.png");
?>

Upvotes: 2

Related Questions