Image Help Please

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

Post Reply
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Image Help Please

Post by xterra »

Ladies and gentlemen,
I read a tutorial from here I believe about uploading images. It works fine in Firefox, but not in IE. I think the problem has to do with the way it saves it. IE cannot save it to the folder (even though I set the folder properties to accessible) but Firefox can. Something with the "\" probably, but I've tried every single possibility. Could someone please look at this below?

The filename is a random hash. The original is replaced by the thumbnail.

Code: Select all

$rand=randomHash();

// Get the details of "imagefile"
$filename = $_FILES['imagefile']['name'];
$temporary_name = $_FILES['imagefile']['tmp_name'];
$mimetype = $_FILES['imagefile']['type'];
$filesize = $_FILES['imagefile']['size'];

//Open the image using the imagecreatefrom..() command based on the MIME type.
switch($mimetype) {
    case "image/jpg":
    case "image/jpeg":
        $i = imagecreatefromjpeg($temporary_name);
        break;
    case "image/gif":
        $i = imagecreatefromgif($temporary_name);
        break;
    case "image/png":
        $i = imagecreatefrompng($temporary_name);
        break;
}

//Delete the uploaded file
unlink($temporary_name);

//Save a copy of the original
imagejpeg($i,"images/$rand.jpg",80);

//Specify the size of the thumbnail
$dest_x = 400;
$dest_y = 400;

//Is the original bigger than the thumbnail dimensions?
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
    //Is the width of the original bigger than the height?
    if (imagesx($i) >= imagesy($i)) {
        $thumb_x = $dest_x;
        $thumb_y = imagesy($i)*($dest_x/imagesx($i));
    } else {
        $thumb_x = imagesx($i)*($dest_y/imagesy($i));
        $thumb_y = $dest_y;
    }
} else {
    //Using the original dimensions
    $thumb_x = imagesx($i);
    $thumb_y = imagesy($i);
}

//Generate a new image at the size of the thumbnail
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);

//Copy the original image data to it using resampling
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));

//Save the thumbnail
imagejpeg($thumb, "images/$rand.jpg", 80);
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

mm..I think you got something wrong
1. You should you getimagesize() to determine image mime type
2. Also for upload use move_uploaded_file()

Check manual for examples.
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post by xterra »

Isn't it strange it's working on Firefox but not IE though? There's no error coming up.


Also, quick question, can I convert that code to resize from a image url? Say the user is not selecting it from his/her computer, it's from another directory.

Thanks.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Well, it is strange it works at all...this is not script for file upload. period.
Tell use what exactly you're trying to achive...
It 99.9% sure it has nothing to do with browser...once you come to serverside..browser is no more relevant... you can check your input ..$_FILES in this case. If value is same when passed from IE and from FF ..it is not browser issue...

I guess you test is inconsistent, hence you think it's browser issue.
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post by xterra »

Tell use what exactly you're trying to achive...
I would like the user to be able to upload one image to a directory. That image will be resized to 100x100, and 400x400 (two copies). The original of course will be deleted.

Thanks.[/quote]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

The mimetype for Internet Explorer is image/pjpeg, so your switch is never being executed.

As mentioned before, use getimagesize() to determine the type of image, rather than the mimetype sent by the browser.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post by xterra »

Thanks but the previous user said that's not even a good upload script? What should I use then?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

It would be all right if you didn't rely on the mime type

switch that out with:

Code: Select all

<?php

//get the image information
if ($imgInfo = @getimagesize($temporary_name))
{
	switch ($imgInfo[2])
	{
		case 1:
		imagegif();
		break;
		
		case 2:
		imagejpeg();
		break;
		
		case 3:
		imagepng();
		break;
		
		default:
		die('Please upload a gif, jpg, or png image');
		break;
	}
} else
{
	die('You did not upload an image');
}
That will get you started in the right direction.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

The least check you should do is is_uploaded_file() on $_FILES['imagefile']['tmp_name'] and not blindly rely on it. Read manual for more.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

IE uses image/pjpeg for a MIME type on jpegs. Because it's weird. scottayy's approach is preferable.
Post Reply