View Full Version : GD Image Library and gif issues.

11-18-2007, 01:37 AM
I have an image uploading thing that creates an images from gif, png, and jpg (jpeg), creates a thumbnail from the created image (jpg), and then watermarks the created image, then saves as jpg. All that works just fine, except when the original images are gif.

For images that were originally gif; the watermark only shows as a white block where the watermark should be (the watermark is a transparent png image). Then there's transparent gifs. The transparency fills with white, but when the thumbnail is created, the transparency fills with black. Anyway I can have the thumbnail fill with white like the original? For animated gifs, it takes the first image in the loop and converts it to the jpg (no surprise here, considering jpg can't be animated). I get the same white block watermark though.

How do I fix these problems, if I can?


$imagedir = $dir."/img/gallery/";
$image = str_replace(array(" ", "/", ".."), "", strtolower($_FILES['image']['name']));
$imagepath = $imagedir.$image;

move_uploaded_file($_FILES['image']['tmp_name'], $imagepath) or die("Unable to upload.");

$img_explode = explode('.', $image);
$img_ext = $img_explode[1];
switch ($img_ext)
case 'jpg': case 'jpeg':
$new_image = imagecreatefromjpeg($imagepath);

case 'png':
$new_image = imagecreatefrompng($imagepath);

case 'gif':
$new_image = imagecreatefromgif($imagepath);

default: die('Unknown file type error.');

$image_width = imagesx($new_image);
$image_height = imagesy($new_image);

$imagenewname = "img".rand(1000000,9999999);
$imagenewpath = $imagedir.$imagenewname.".jpg";

// start thumbnail creation process
$thumbdir = $imagedir."thumbs/";

$ratio = ($image_width > $image_height) ? (150 / $image_width) : (150 / $image_height);
$th_width = $image_width * $ratio;
$th_height = $image_height * $ratio;

$thumb = imagecreatetruecolor($th_width, $th_height);
imageantialias($thumb, true);

$thumbpath = $thumbdir.$imagenewname.".jpg";
imagecopyresampled($thumb, $new_image, 0, 0, 0, 0, $th_width, $th_height, $image_width, $image_height);
imagejpeg($thumb, $thumbpath, 100);

// end thumbnail creation process

// start watermarking process
$wm = $dir."/img/watermark.png";

$watermark = imagecreatefrompng($wm);

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

imagealphablending($new_image, true);
imagealphablending($watermark, true);

imagecopy($new_image, $watermark, ($image_width - $watermark_width - 5), ($image_height - $watermark_height - 5), 0, 0, $watermark_width, $watermark_height);
imagejpeg($new_image, $imagenewpath);

// end watermarking process