andrew_biggart Posted July 24, 2012 Share Posted July 24, 2012 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 https://forums.phpfreaks.com/topic/266172-creating-transparent-gifs-with-php/ Share on other sites More sharing options...
andrew_biggart Posted July 25, 2012 Author Share Posted July 25, 2012 Does anyone have any advice? Link to comment https://forums.phpfreaks.com/topic/266172-creating-transparent-gifs-with-php/#findComment-1364221 Share on other sites More sharing options...
xyph Posted July 25, 2012 Share Posted July 25, 2012 There's plenty on Google about this. GIF's alpha isn't a 'channel', it's a 'colour' on the palette, so it works a little differently, and will give you potentially UGLY results. Link to comment https://forums.phpfreaks.com/topic/266172-creating-transparent-gifs-with-php/#findComment-1364263 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.