Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Hello everyone and congrats to getting the forum back!
I have a small problem. I have created a script to upload an image from the users computer. It should upload it, then resize the picture (this is because people tend to upload 2Meg + files from their digital cameras) then copy it to a directory.
The problem I am having is that it works fine with JPG images but not GIF. Can anyone see why this might be or suggest how I can change this code to allow it?Code: Select all
<?php require_once('../../php/page_functions.php');
require_once('../../Connections/db_connect.php');
require_once('../../php/conversion_functions.php');
require_once('../../php/download_functions.php');
$error = '0';
// restrict_access(access_level,redirect_URL,Username);
if (!empty($_SERVER['QUERY_STRING'])) {
$url = $PHP_SELF."?".$_SERVER['QUERY_STRING'];
} else {
$url = $PHP_SELF;
}
restrict_access(6,$url,$_SESSION['MM_Username']);
// end restrict_access
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
@$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
@$RESIZEHEIGHT=true;
}
if(@$RESIZEWIDTH && @$RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif(@$RESIZEWIDTH){
$ratio = $widthratio;
}elseif(@$RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}
$file_name = $HTTP_POST_FILES['file']['name'];
if ($file_name['file']['name'] = "JPG") { $file_name['file']['name'] = "jpg"; }
$type = get_file_type($file_name);
switch ($type) {
case "jpg":
break;
case "JPG":
break;
case "jpeg":
break;
default:
$error = "2";
}
if(empty($_POST['question'])) { $error = '1'; }
if ($error == '0') {
//inserting data
mysql_select_db($database_db_connect, $db_connect);
$insert_sql = "insert into quizzes(quiz_id, question_number, question, multi_options, image, max_options) values('$quiz_id', '$question_id', '$question', '$multiple_answers','$file_name', '$max_correct_answers')";
mysql_query($insert_sql, $db_connect) or die(mysql_error());
// begin_new_script;
// Filename to store image as (no extention)
$FILENAME= "../../images/quizzes/quiz/".$quiz_id."/".$file_name;
// Width to reszie image to (in pixels)
$RESIZEWIDTH=300;
// Width to reszie image to (in pixels)
$RESIZEHEIGHT=255;
// DO NOT EDIT BELOW HERE ---------------------------------------
if($_FILES['file']['size']){
if($_FILES['file']['type'] == "image/pjpeg" || $_FILES['file']['type'] == "image/jpeg"){
$im = imagecreatefromjpeg($_FILES['file']['tmp_name']);
}elseif($_FILES['file']['type'] == "image/x-png" || $_FILES['file']['type'] == "image/png"){
$im = imagecreatefrompng($_FILES['file']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['file']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME")){
unlink("$FILENAME");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
ImageDestroy ($im);
}
}
$urlstring="add4.php?quiz_id=".$quiz_id."&question_id=".$question_id."&no_of_options=".$no_of_options;
header("Location:$urlstring");
} else { header("Location:add3.php?quiz_id=$quiz_id&error=$error"); }
?>