Image Upload problem

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
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Image Upload problem

Post by sulen »

Weirdan | Please use

Code: Select all

and

Code: 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]
I have written an image upload module which to upload an image, resize it and then create a thumbnail for it based on certain dimensions. The issue is that the script works perfectly in Firefox but generates a whole bunch of image resource errors in IE. The code is pasted below. Any help will be appreciated. The errors that I recieve are

Code: Select all

Warning: imagesx(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1769
Warning: imagesy(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1769
Warning: imagesx(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1778
Warning: imagesy(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1779
Warning: imagecreatetruecolor(): Invalid image dimensions in /home/soderbrg/public_html/admin.php on line 1782
Warning: imagesx(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1783
Warning: imagesy(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1783
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1783
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1785
Warning: imagesx(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1791
Warning: imagesy(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1791
Warning: imagesx(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1800
Warning: imagesy(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1801
Warning: imagecreatetruecolor(): Invalid image dimensions in /home/soderbrg/public_html/admin.php on line 1804
Warning: imagesx(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1805
Warning: imagesy(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1805
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1805
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/soderbrg/public_html/admin.php on line 1806

Code: Select all

if ($action=='uploadimg') {
if (session_is_registered("uid")) {
$prod_id=$_GET['prod_id'];

$filename = $_FILES['imagefile']['name'];
$temporary_name = $_FILES['imagefile']['tmp_name'];
$mimetype = $_FILES['imagefile']['type'];
$filesize = $_FILES['imagefile']['size'];
$split = explode(".", $filename);
$name=$split[0];

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;
}

unlink($temporary_name);

//imagejpeg($i,"images/uploadedfile.jpg",80);

//Specify the size of the new product image
$dest_x = 200;
$dest_y = 300;

if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
    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 {
    $thumb_x = imagesx($i);
    $thumb_y = imagesy($i);
}

$thumb = imagecreatetruecolor($thumb_x,$thumb_y);
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
//Save the product image
imagejpeg($thumb, "images/".$name.".jpg", 80);

//Creating the thumbnail image
$dest_x = 75;
$dest_y = 75;

if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
    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 {
    $thumb_x = imagesx($i);
    $thumb_y = imagesy($i);
}

$thumb = imagecreatetruecolor($thumb_x,$thumb_y);
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
imagejpeg($thumb, "images/".$name."_th.jpg", 80);

$sql="update product_pages set prod_image='$filename', prod_image_th='".$name."_th.jpg' where prod_id='".$prod_id."'";
$result = mysql_query($sql);

?>
<tr>
<td class="text" width="100%" align="center">The Image  : <b><?=$filename?></b> has been added for the Product.</td>
</tr>
<tr>
<td class='text' align='center'><a href="admin.php?action=uploadfile&prod_id=<?=$prod_id?>" class="link">Click here to Upload a Document</a> | <a href="admin.php?action=admin" class="link">Admin Main Page</a></td>
</tr>
<?

} else {
?>
<script LANGUAGE="JavaScript">
<!--
location='admin.php?action=login ';
// -->
</script>
<?
}
}
Weirdan | Please use

Code: Select all

and

Code: 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]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I don't think your switch is getting evaluated do to this line

case "image/jpg":

perhaps take that out,
or add a

// nothing
break;

after it

to see if $i is being read, try var_dump($i) .. or if(!isset($i)){ echo '$i not evaluated'; }
Last edited by s.dot on Wed Mar 29, 2006 2:45 am, edited 1 time in total.
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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Also, it might be worthwhile to put this into a function and include it.

It would make looking for errors a lot easier in your ~2000 lines of code :)
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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

IE uploads jpegs with a MIME type of "image/pjpeg".

Coz it's weird.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

scottayy wrote:I don't think your switch is getting evaluated do to this line

case "image/jpg":

perhaps take that out,
or add a

// nothing
break;
That's a switch case fall through .. it's fine. It's just a way of making two different cases do the same thing.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Relying on the submission to tell you the mime-type can bite you in the ass. Use getimagesize() to determine the mime-type.
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

I added this to the switch and it worked on IE

Code: Select all

switch($mimetype) {
    case "image/jpg":
    case "image/jpeg":
		case "image/pjpeg":
        $i = imagecreatefromjpeg($temporary_name);
        break;
    case "image/gif":
        $i = imagecreatefromgif($temporary_name);
        break;
    case "image/png":
        $i = imagecreatefrompng($temporary_name);
        break;
}
Thanks a tonne for the ideas.
Post Reply