Jump to content

Creating transparent gifs with php


andrew_biggart

Recommended Posts

I am currently trying to create an image uploading script that resizes the image if it over 570 pixels wide and saves the orignal image plus two different sized copies. I have got the script recreating jpg and png images, however when I upload a gif image the sized image no longer have a transparent background, they have a black background.

 

Can anyone advise me what I am doing wrong please?

 

<?php
session_start();

$upload_dir   = 'uploads/';
$medium_dir   = 'medium/';
$thumbs_dir   = 'thumbs/';
$medium_width = 570;
$thumb_width  = 50;

$allowed_ext = array('jpg','jpeg', 'png','gif');
date_default_timezone_set('Europe/Dublin');
$unique_id   = date("YmdHis");
$unique_id   = $unique_id . rand(0,999999999);

if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
	exit_status('Error! Wrong HTTP method!');
}


if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){

	$file_tmp   = $_FILES['pic']['tmp_name'];
	$file_name  = $_FILES['pic']['name'];
	$file_size  = $_FILES['pic']['size'];
	$file_ext   = get_extension($_FILES['pic']['name']);
	$file_new   = $unique_id . "." . $file_ext;
	$createdby  = $_SESSION["ufullname"];

	if(!in_array(get_extension($file_name),$allowed_ext)){
		exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
	}	

	// Check the width of the uploaded image
	list($file_width,$file_height) = getimagesize($file_tmp);

	if($file_width > $medium_width) {

		// Move the uploaded file from the temporary 
		// directory to the uploads folder:

		if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){

			include('functions.php');

			connect();

			$link   = get_option('admin_url');

			$sql    = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) ";
			$result = mysql_query($sql);


			if($result) {

				if($file_ext == "jpg" || $file_ext == "jpeg" ){
					$src = imagecreatefromjpeg($upload_dir . $file_new);
				}
				else if($file_ext == "png"){
					$src = imagecreatefrompng($upload_dir . $file_new);
				}
				else if($file_ext == "gif"){
					$src = imagecreatefromgif($upload_dir . $file_new);
				}

				//Create medium image.

				$file_height_medium = ($file_height/$file_width) * $medium_width;
				$medium_image = imagecreatetruecolor($medium_width,$file_height_medium);

				//Create small image.

				$file_height_thumb = ($file_height/$file_width) * $thumb_width;
				$thumb_image = imagecreatetruecolor($thumb_width,$file_height_thumb);


				if($file_ext == "jpg" || $file_ext == "jpeg" ){
					imagecopyresized($medium_image,$src,0,0,0,0,$medium_width,$file_height_medium,$file_width,$file_height);
					imagecopyresized($thumb_image,$src,0,0,0,0,$thumb_width,$file_height_thumb,$file_width,$file_height);
					imagejpeg($medium_image,$upload_dir . $medium_dir . $file_new,100);
					imagejpeg($thumb_image,$upload_dir . $thumbs_dir . $file_new,100);
				}
				else if($file_ext == "png"){
					imagesavealpha($medium_image, true); 
					imagesavealpha($thumb_image, true);

					$color = imagecolorallocatealpha($medium_image,0x00,0x00,0x00,127); 
					$color2 = imagecolorallocatealpha($thumb_image,0x00,0x00,0x00,127);
					imagefill($medium_image, 0, 0, $color);
					imagefill($thumb_image, 0, 0, $color2);
					imagecopyresized($medium_image,$src,0,0,0,0,$medium_width,$file_height_medium,$file_width,$file_height);
					imagecopyresized($thumb_image,$src,0,0,0,0,$thumb_width,$file_height_thumb,$file_width,$file_height);
					imagepng($medium_image,$upload_dir . $medium_dir . $file_new);
					imagepng($thumb_image,$upload_dir . $thumbs_dir . $file_new);
				}
				else if($file_ext == "gif"){
					imagesavealpha($medium_image, true); 
					imagesavealpha($thumb_image, true);

					$color = imagecolorallocatealpha($medium_image,0x00,0x00,0x00,127);
					$color2 = imagecolorallocatealpha($thumb_image,0x00,0x00,0x00,127); 
					imagefill($medium_image, 0, 0, $color);
					imagefill($thumb_image, 0, 0, $color2);
					imagecopyresized($medium_image,$src,0,0,0,0,$medium_width,$file_height_medium,$file_width,$file_height);
					imagecopyresized($thumb_image,$src,0,0,0,0,$thumb_width,$file_height_thumb,$file_width,$file_height);
					imagegif($medium_image,$upload_dir . $medium_dir . $file_new);
					imagegif($thumb_image,$upload_dir . $thumbs_dir . $file_new);
				}

				exit_status('File was uploaded successfuly!');

				imagedestroy($src);
				imagedestroy($medium_image);
				imagedestroy($thumb_image);	

			}
			else {
				exit_status('Error! <i>File couldnt be saved to the database.</i>');
			}
		}
	}
	else {


		// Move the uploaded file from the temporary 
		// directory to the uploads folder:

		if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){

			include('functions.php');

			connect();

			$link   = get_option('admin_url');

			$sql    = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) ";
			$result = mysql_query($sql);

			exit_status('File was uploaded successfuly!');
		}
	}

}

exit_status('Something went wrong with your upload!');


// Helper functions

function exit_status($str){
	echo json_encode(array('status'=>$str));
	exit;
}

function get_extension($file_name){
	$ext = explode('.', $file_name);
	$ext = array_pop($ext);
	return strtolower($ext);
}
?>

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.