Page 1 of 1

Image Upload problem

Posted: Wed Mar 29, 2006 12:59 am
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]

Posted: Wed Mar 29, 2006 2:42 am
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'; }

Posted: Wed Mar 29, 2006 2:43 am
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 :)

Posted: Wed Mar 29, 2006 3:12 am
by onion2k
IE uploads jpegs with a MIME type of "image/pjpeg".

Coz it's weird.

Posted: Wed Mar 29, 2006 3:14 am
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.

Posted: Wed Mar 29, 2006 9:06 am
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.

Posted: Wed Mar 29, 2006 11:58 am
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.