PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
shanecody
Forum Newbie
Posts: 23 Joined: Fri May 08, 2009 4:12 pm
Post
by shanecody » Wed Jul 08, 2009 5:46 pm
I wrote a script to re size an image that has been uploaded to a web site. It works perfectly using Firefox, in Internet Explorer it doesn't upload corectly.
Anyone know why?
Code: Select all
function resizeImage($file, $type, $newwidth, $newheight){
// test for image type chooses appropriate image function to use
if($type == "image/jpeg"){
$src = imagecreatefromjpeg($file);
} elseif($type == "image/gif") {
$src = imagecreatefromgif($file);
} elseif($type == "image/png") {
$src = imagecreatefrompng($file);
} else {
die("<h2> Unsuported image format - supported formats are .jpg, .gif, and .png</h2>");
}
list($width,$height) = getimagesize($file);
// create temp image and copy resize image to it
$tmp = imagecreatetruecolor($newwidth, $newheight);
if(imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height)){
$result = true;
} else {
$result = false;
}
// test for image type chooses appropriate image function to use
if($type == "image/jpeg"){
imagejpeg($tmp, $file, 100);
} elseif($type == "image/gif") {
imagegif($tmp, $file, 100);
} else {
imagepng($tmp, $file, 100);
}
// destroy temp images
imagedestroy($src);
imagedestroy($tmp);
return $result;
}
$image = getImage('file_upload', true, 200, 300);
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Jul 08, 2009 10:03 pm
With that little bit of code you posted? Heck no.
I'd bet that it has to do with your code assuming the browser submits a (correct) file MIME type with the upload. That's $_FILES[...][type] by the way.
That file type can't be trusted - don't bother using it. Ever.
Either determine the file type yourself or assume that the user isn't stupid and gave the file the right extension. Worst case is that the imagecreatefrom* function fails to open it.
shanecody
Forum Newbie
Posts: 23 Joined: Fri May 08, 2009 4:12 pm
Post
by shanecody » Thu Jul 09, 2009 11:32 am
Sorry I forgot to post the other function. I did assume that the user wasn't stupid, and if I take the exact same file and upload it with Firefox, works perfectly, do it with IE and it fails. How can I determine file type without $_FILES[...][type]?
Code: Select all
function getImage($file_upload, $resize = false, $maxwidth = 0, $maxheight = 0){
if($_FILES[$file_upload]['error'] == UPLOAD_ERR_OK)
{
// process the form data
$tmp_file = $_FILES[$file_upload]['tmp_name'];
$type = $_FILES[$file_upload]['type'];
// calls to resize the image if $resize == true
if($resize == true){
list($width,$height) = getimagesize($tmp_file);
if($width > $maxwidth){
$newwidth = $maxwidth;
$newheight = ($height/$width) * $newwidth;
resizeImage($tmp_file, $type, $newwidth, $newheight);
if($newheight > $maxheight) {
$newheight = $maxheight;
$newwidth = ($width/$height) * $newheight;
resizeImage($tmp_file, $type, $newwidth, $newheight);
}
} elseif($height > $maxheight) {
$newheight = $maxheight;
$newwidth = ($width/$height) * $newheight;
resizeImage($tmp_file, $type, $newwidth, $newheight);
}
}
$fp = base64_encode(file_get_contents($tmp_file));
if($fp)
return $fp;
else {
$message = array('error' => 'File not converted');
return $message;
}
} else {
$upload_errors = array(
UPLOAD_ERR_OK => "No errors.",
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL => "Partial upload.",
UPLOAD_ERR_NO_FILE => "No file.",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
UPLOAD_ERR_EXTENSION => "File upload stopped by extension."
);
$error = $_FILES[$file_upload]['error'];
$message = array('error' => $upload_errors[$error]);
//echo $message['error'];
return $message;
}
}
shanecody
Forum Newbie
Posts: 23 Joined: Fri May 08, 2009 4:12 pm
Post
by shanecody » Thu Jul 09, 2009 11:43 am
Never mind fount the solution.
Code: Select all
$type = mime_content_type($_FILES[$file_upload]['tmp_name']);