gojo62 Posted March 28, 2011 Share Posted March 28, 2011 This code is taken from the book Learning Php Mysql & javascript. It converts an image into a thumbnail 100 by 100 and works fine in chrome but in IE8 it does not resize PNG images but saves them at original size. I have spent a day searching the internet for answers and even tried joining the books forum but had problems logging into it! If anyone knows why PNG images are not getting resized in IE8 I would be deeply grateful. <?php // rnprofile.php include_once 'rnheader.php'; if (!isset($_SESSION['user'])) die("<br /><br />You need to login to view this page"); $user = $_SESSION['user']; echo "<h3>Edit your Profile</h3>"; if (isset($_POST['text'])) { $text = sanitizeString($_POST['text']); $text = preg_replace('/\s\s+/', ' ', $text); $query = "SELECT * FROM rnprofiles WHERE user='$user'"; if (mysql_num_rows(queryMysql($query))) { queryMysql("UPDATE rnprofiles SET text='$text' where user='$user'"); } else { $query = "INSERT INTO rnprofiles VALUES('$user', '$text')"; queryMysql($query); } } else { $query = "SELECT * FROM rnprofiles WHERE user='$user'"; $result = queryMysql($query); if (mysql_num_rows($result)) { $row = mysql_fetch_row($result); $text = stripslashes($row[1]); } else $text = ""; } $text = stripslashes(preg_replace('/\s\s+/', ' ', $text)); if (isset($_FILES['image']['name'])) { $saveto = "$user.jpg"; move_uploaded_file($_FILES['image']['tmp_name'], $saveto); $typeok = TRUE; switch($_FILES['image']['type']) { case "image/gif": $src = imagecreatefromgif($saveto); break; case "image/jpeg": // Both regular and progressive jpegs case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break; case "image/png": $src = imagecreatefrompng($saveto); break; default: $typeok = FALSE; break; } if ($typeok) { list($w, $h) = getimagesize($saveto); $max = 100; $tw = $w; $th = $h; if ($w > $h && $max < $w) { $th = $max / $w * $h; $tw = $max; } elseif ($h > $w && $max < $h) { $tw = $max / $h * $w; $th = $max; } elseif ($max < $w) { $tw = $th = $max; } $tmp = imagecreatetruecolor($tw, $th)or die('Cannot Initialize new GD image stream'); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h); imageconvolution($tmp, array( // Sharpen image array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1) ), 8, 0); imagejpeg($tmp, $saveto); imagedestroy($tmp); imagedestroy($src); } } showProfile($user); echo <<<_END <form method='post' action='rnprofile.php' enctype='multipart/form-data'> Enter or edit your details and/or upload an image:<br /> <textarea name='text' cols='40' rows='3'>$text</textarea><br /> Image: <input type='file' name='image' size='14' maxlength='32' /> <input type='submit' value='Save Profile' /> </pre></form> _END; ?> Thanks Gary Quote Link to comment https://forums.phpfreaks.com/topic/231933-imagecopyresampled-problem-resizing-in-ie/ Share on other sites More sharing options...
jggretton Posted March 28, 2011 Share Posted March 28, 2011 If it's acting differently in IE and Chrome it's likely to be a problem with how the browser is displaying the images. Could you open the converted image directly in each browser and press refresh (ie go to http://www.yoursite.com/image-folder/converted-image.png) and confirm that they are really different? Quote Link to comment https://forums.phpfreaks.com/topic/231933-imagecopyresampled-problem-resizing-in-ie/#findComment-1193222 Share on other sites More sharing options...
gojo62 Posted March 28, 2011 Author Share Posted March 28, 2011 The images on the server are as follows original png 600w 300h 14kb ie8 uploaded 600w 300h 14kb chrome uploaded 100w 60h 4kb So IE8 hasn't altered it at all while chrome has this also the case for all the other png's I have tried while jpegs have been made a lot small. Quote Link to comment https://forums.phpfreaks.com/topic/231933-imagecopyresampled-problem-resizing-in-ie/#findComment-1193236 Share on other sites More sharing options...
kenrbnsn Posted March 28, 2011 Share Posted March 28, 2011 Since you're only doing the resize if the type of the uploaded file matches, then the type that's being passed from IE is not matching. I have a short script that uploads files and tells you the type of the uploaded file. <html> <head> <title>Test Upload</title> </head> <body> <?php if (isset($_POST['submit'])) { echo '<pre>' . print_r($_POST,true) . '</pre>'; echo '<pre>' . print_r($_FILES,true) . '</pre>'; } ?> <form action="" method="post" enctype="multipart/form-data"> <p> <input type="file" name="filename"> <input type="submit" name="submit" value="Upload"> </p> </form> </body> </html> You can see it in action at: http://rbnsn.com/phpfreaks/test_upload.php Ken Quote Link to comment https://forums.phpfreaks.com/topic/231933-imagecopyresampled-problem-resizing-in-ie/#findComment-1193257 Share on other sites More sharing options...
jggretton Posted March 28, 2011 Share Posted March 28, 2011 Good call Ken, so gojo could fix this by adding the following line: $_FILES['image']['type'] = image_type_to_mime_type(exif_imagetype($saveto)); Just after the move_uploaded_file statement. So it would read: move_uploaded_file($_FILES['image']['tmp_name'], $saveto); $_FILES['image']['type'] = image_type_to_mime_type(exif_imagetype($saveto)); Hopefully that should do the trick! Quote Link to comment https://forums.phpfreaks.com/topic/231933-imagecopyresampled-problem-resizing-in-ie/#findComment-1193264 Share on other sites More sharing options...
gojo62 Posted March 29, 2011 Author Share Posted March 29, 2011 Thanks Guys I will go and test these later. Gary Quote Link to comment https://forums.phpfreaks.com/topic/231933-imagecopyresampled-problem-resizing-in-ie/#findComment-1193566 Share on other sites More sharing options...
gojo62 Posted March 29, 2011 Author Share Posted March 29, 2011 Hi Guys Added line of code supplied by jggretton now chrome shows blank page and IE8 shows code as shown below <html><head><title>Robin's Nest (a1)</title></head><body><font face='verdana' size='2'><h2>Robin's Nest</h2><b>a1</b>: <a href='rnmembers.php?view=a1'>Home</a> | <a href='rnmembers.php'>Members</a> | <a href='rnfriends.php'>Friends</a> | <a href='rnmessages.php'>Messages</a> | <a href='rnprofile.php'>Profile</a> | <a href='rnlogout.php'>Log out</a><h3>Edit your Profile</h3><img src='a1.jpg' border='1' align='left'/>aa<br clear=left /><br /> if I add [codeecho '<pre>' . print_r($_POST,true) . '</pre>'; echo '<pre>' . print_r($_FILES,true) . '</pre>';] I get above text on page plus this underneath <pre>Array ( [text] => aa ) </pre><pre>Array ( [image] => Array ( [name] => book.png [type] => image/png [tmp_name] => E:\UniServer\tmp\phpE267.tmp [error] => 0 => 45358 ) ) </pre><form method='post' action='rnprofile.php' enctype='multipart/form-data'> Enter or edit your details and/or upload an image:<br /> <textarea name='text' cols='40' rows='3'>aa</textarea><br /> Image: <input type='file' name='image' size='14' maxlength='32' /> <input type='submit' value='Save Profile' /> </pre></form> If I log back into robinsnest the image has successfully been altered but I guess I still have a underline problem. Gary Quote Link to comment https://forums.phpfreaks.com/topic/231933-imagecopyresampled-problem-resizing-in-ie/#findComment-1193588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.