Click to See Complete Forum and Search --> : IE Bug using PHP to Create Image


mididelight
03-16-2006, 08:30 AM
Hello,

I am using the script found on this page (http://www.devnetwork.net/forums/viewtopic.php?t=41743) to take a very large image and use php to make it smaller. I actually am doing it twice, once to resize the image to the "enlarge" version and then again to make a thumbnail of that. Everything works great in Firefox but when I try to upload the image and run the script in IE 6.0/win I get an unforunate error.

It says imagesx($i): argument supplied is not a valid image. ( I paraphrased ). Here is the code I am using. I am not sure what is going on here. Why would IE bail? This isnt CSS, why should the browser I'm using matter?

<?php
if ($_POST["upload"] == "upload") {

$category = $_POST["category"];

// Connect to DB to get image name
dbConnect('dbname');
// SQL
$sql = mysql_query(" SQL QUERY");

// Get Name
while ($row = mysql_fetch_array($sql)) {
$num = $row["name"] + 1;
}

// Concatenate folder name and file name
$newthumbname = "../images/".$category."/".$num.".jpg";
$newenlargename = "../images/".$category."/large/".$num.".jpg";


// ================================== Create Large Image ================================

// Get Image Properties
$filename = $_FILES['imagefile']['name'];
$temporary_name = $_FILES['imagefile']['tmp_name'];
$mimetype = $_FILES['imagefile']['type'];
$filesize = $_FILES['imagefile']['size'];

// Create Image from 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;
}

// Set Width and Height
$dest_x = 430; //width
$dest_y = 400; //height

// Get Ratio
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
$enlarge_x = $dest_x;
$enlarge_y = imagesy($i)*($dest_x/imagesx($i));
} else {
$enlarge_x = imagesx($i);
$enlarge_y = imagesy($i);
}

// Create New Image
$enlarge = imagecreatetruecolor($dest_x,$dest_y);
// Resize it
imagecopyresampled($enlarge, $i ,0, 0, 0, 0, $enlarge_x, $enlarge_y, imagesx($i), imagesy($i));
// Save It
imagejpeg($enlarge, $newenlargename, 80);


// ================================== Create Thumbnail ================================

//unlink($temporary_name);

// Set Width and Height
$tdest_x = 100; //width
$tdest_y = 66; //height

// Get Ratio
if (imagesx($i) > $tdest_x or imagesy($i) > $tdest_y) {
$thumb_x = $tdest_x;
$thumb_y = imagesy($i)*($tdest_x/imagesx($i));
} else {
$thumb_x = imagesx($i);
$thumb_y = imagesy($i);
}

// Create New Image
$thumb = imagecreatetruecolor($tdest_x,$tdest_y);
// Resize it
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
// Save It
imagejpeg($thumb, $newthumbname, 80);


// Insert into DB
$insert = mysql_query(" INSERT INTO gallery (name, category, active) VALUES ('$num', '$category', '0') ") or die( "<b>Error:</b> Something went wrong, could not edit link status.". mysql_error());

if ($insert) { echo "<p><strong>New Image Uploaded</strong></p>"; }


// Close DB
@mysql_close($dbcnx);

}

?>

I might be using this wrong, but I basically go through the script twice, so I can create both of my images.

Any ideas?

chazzy
03-16-2006, 01:22 PM
Try having it set a content type.

mididelight
03-16-2006, 03:29 PM
i'm confused, have what set a content type?

chazzy
03-16-2006, 03:40 PM
ohhh haha read it backwards.

it's not that you're not sending IE a mimetype, i think it's probable that it's the other way around -IE's not sending you a mimetype.

before this section:

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


try seeing what mimetype IE does send by just echoing it:

echo "The mime type is: ".$temporary_name = $_FILES['imagefile']['tmp_name']."<br />";

Also, don't paraphrase your error messages. Just copy and paste it.

mididelight
03-16-2006, 05:36 PM
ok so i echoed out the mime type in IE and I got nothing back, so you were right. How do I get IE to get the mime type? Here is the error I am getting by the way:

The mime type is: <------ left blank

Warning: imagesx(): supplied argument is not a valid Image resource in

chazzy
03-16-2006, 08:55 PM
you could fall back to looking at the extension of the filename. just make the default case have a set of subcases that execute based on .jpg, .png, .gif, etc.