By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

image resize - confused (code)

P: 67
Ive been searching for an image resize tutorial for a while now and found this code which worked nicely. I was unable to find the full source code but i think i pieced together the code from the tutorials correctly. My problem is when i upload an image it does not fit any case in the open_image function switch statement.

when i echo $file i get 0808...tmp on my localhost server and 0808.... on my webhosting server. There is no .jpg even if the file i upload is .jpg I understand why this doesnot fit the switch statement however it seems to work fine for the tutorial.

[PHP]
function open_image ($file) {
// Get extension
$extension = strrchr($file, '.');
$extension = strtolower($extension);

switch($extension) {
case '.jpg':
case '.jpeg':
$im = @imagecreatefromjpeg($file);
break;
case '.gif':
$im = @imagecreatefromgif($file);
break;
default:
$im = false;
break;
}
return $im;
}


if ($_POST) {
// No image?
if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
die ('<strong>Invalid image uploaded. Please go back and try again.</strong>');
}
$imagepath = $_FILES['image']['tmp_name'];
echo $imagepath;
// Load image
$image = open_image($imagepath);
if ($image == false) {
die ('<strong>You uploaded an invalid image. Please go back and try again.</strong>');
}

$new_width = floatval($_POST['new_width']);
$new_height = $height * ($new_width/$width);

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Display resized image
header('Content-type: image/jpeg');
imagejpeg($image_resized);
die();
}
[/PHP]
Aug 2 '07 #1
Share this Question
Share on Google+
12 Replies


kovik
Expert 100+
P: 1,044
That script does not look like it was meant for uploads, and if it was, then it is disgustingly insecure.

In order to check a file's type, you'll want to use a combination of getimagesize() and the image creation functions that are already being used.

So, use getimagesize() to replace the extension lookup, and use the file's actual headers.
Aug 2 '07 #2

P: 67
wow getimagesize is alot easier... i am having a problem with output after @imagecreatefromjpeg ... (what does the @ mean) if i put the echo statement before the @imagecreat..... it will output, after however i get no output.
any thoughts.

Thanks for the help.


[PHP] if ($_POST) {
// No image?
if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
die ('<strong>Invalid image uploaded. Please go back and try again.</strong>');
}
$imagepath = $_FILES['image']['tmp_name'];
$imageinfo = getimagesize($imagepath);

if($imageinfo[2] == 2){
$image = @imagecreatefromjpeg($imagepath);
}
echo "test";
}
[/PHP]
Aug 2 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, Shawn.

The '@' tells PHP to suppress any errors or warnings generated by the function following the '@' symbol. You will definitely want to remove those ASAP!
Aug 2 '07 #4

P: 67
haha, ok that makes sense, thanks

Heya, Shawn.

The '@' tells PHP to suppress any errors or warnings generated by the function following the '@' symbol. You will definitely want to remove those ASAP!
Aug 2 '07 #5

pbmods
Expert 5K+
P: 5,821
Heya, Shawn.

If you're getting a blank page, your script is probably generating an error. Check out this article to find out what is going on.
Aug 3 '07 #6

kovik
Expert 100+
P: 1,044
wow getimagesize is alot easier... i am having a problem with output after @imagecreatefromjpeg ... (what does the @ mean) if i put the echo statement before the @imagecreat..... it will output, after however i get no output.
any thoughts.
imagecreatefrom* functions do not create output, they only create image resources.
Aug 3 '07 #7

P: 67
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 9216 bytes) in D:\web\htdocs\websites\uploadtest.php on line 13

This is the error i get when removing the @ before the imagecreatefromjpeg
any thoughts? I read something about this error, it said to create a .htaccess file and put the following code in it:

php_value memory_limit 150M
php_value post_max_size 150M

i did this and then saved the file in the same directory as my php file... to no avail.
Aug 3 '07 #8

pbmods
Expert 5K+
P: 5,821
Heya, Shawn.

What does your code look like right now?
Aug 3 '07 #9

kovik
Expert 100+
P: 1,044
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 9216 bytes) in D:\web\htdocs\websites\uploadtest.php on line 13

This is the error i get when removing the @ before the imagecreatefromjpeg
any thoughts? I read something about this error, it said to create a .htaccess file and put the following code in it:

php_value memory_limit 150M
php_value post_max_size 150M

i did this and then saved the file in the same directory as my php file... to no avail.
I tend to leave error suppression on GD functions, and handle the errors myself (you have to check, though!). You have to check if the imagecreatefrom* functions fail, because that's how you know if a file matches it's headers or not, and whether or not it is valid.
Aug 3 '07 #10

P: 67
thanks for all the help, heres what ive got so far

The error_reporting.... did not seem to do much, i added the
or die(mysql_error())...
which does nothing with or without a @ infront of the GD function.

again the output is:
D:\web\PHP\uploadtemp\php19.tmp
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 7212 bytes) in D:\web\htdocs\websites\uploadtest.php on line 13

also usually with errors i have experienced so far any output after the problem area tends to work, in this case it does not. (the echo "test" at the end)

[PHP]<?php
error_reporting(E_ALL);
ini_set('display_errors', True);
if ($_POST) {
// No image?
if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
die ('<strong>Invalid image uploaded. Please go back and try again.</strong>');
}
$imagepath = $_FILES['image']['tmp_name'];
$imageinfo = getimagesize($imagepath);
echo $imagepath;
if($imageinfo[2] == 2){
$image = (imagecreatefromjpeg($imagepath) or die(mysql_error()));
}
echo "test";

}
?>
<html>
<head>
<title>Image Resizer</title>
<style type="text/css">
th { text-align: right; }
</style>

</head>
<body>
<form method="POST" enctype="multipart/form-data">
<table>
<tr>
<th>Image:</th>
<td><input type="file" name="image"></td>
</tr>

<tr>
<th>Resize by: </th>
<td><input type="text" name="percent" size="1" />% (percentage)</td>
</tr>

<tr>
<th>OR new width: </th>
<td><input type="text" name="new_width" size="1" /> pixels (height will be calculated automatically)</td>
</tr>

<tr>
<th>OR new height: </th>
<td><input type="text" name="new_height" size="1" /> pixels (width will be calculated automatically)</td>
</tr>

<tr>
<th>OR new height and new width: </th>
<td>
<table>
<tr><td>width:</td><td><input type="text" name="width" size="1" /> pixels</td></tr>
<tr><td>height:</td><td><input type="text" name="height" size="1" /> pixels</td></tr>
</table>
</td>
</tr>

<tr><td colspan="2" style="text-align:center;"><input type="submit" value="Resize Image" /></td></tr>
</form>
</body>
</html>
[/PHP]
Aug 3 '07 #11

P: 67
found a fix:
ini_set("memory_limit","13M");

i put that at the top of my code. is it strange that i need 13M of space (12M did not work) the file i am processing is under 1Mb.

I also tried larger files like 1.9Mb and this was not sufficient space. is there a way to determine how much space i need?
Aug 3 '07 #12

kovik
Expert 100+
P: 1,044
You should place a limit on how large of a file user's can upload.
Aug 4 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.