473,287 Members | 3,181 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Error Creating an Image from an Uploaded File

I was hopeing someone could help me out here. Been stumped on this
one all day.

This function
1. Checks uploaded files.
2. Creates two resized images from each (a full size, and a
thumbnail).
3. Returns an array with a bool (false if the upload failed), and an
error message.

I am having trouble createing a copy of the image from the functions
imagecreatefrom*

Here is the function:

<?php
function process_images() {
//options
$max_bytes = 65536; // max file size
$max_h = 425; // maximum hieght for images
$max_w = 425; // maximum width for images
$t_h = 125; // max hieght for thumbnails
$t_w = 50; // max width for thumbnails
$max = 2; // max number of images

$i = 1;
while((list($k,$err) = each($_FILES['image']['error'])) and $i<=
$max) {
//check for an error
if($err != UPLOAD_ERR_OK) { return array(false,"There was an
error uploading Image #$i."); }
//TODO: Need better error message for errors here

//get file information
$name = $_FILES['image']['name'][$k];
$type = $_FILES['image']['type'][$k];
$temp = $_FILES['image']['tmp_name'][$k];
$size = $_FILES['image']['size'][$k];

//check that this file is an uploaded file
if(!is_uploaded_file($temp)) { return array(false,"There was
an error uploading Image #$i."); }
//check file size
if($size $max_bytes) { return array(false, "Image #$i
exceeds maximum size"); }

//check type and create a new image based off it
switch($type) {
//jpeg
case "image/pjpeg" :
case "image/jpeg" :
case "image/jpg" : $new_img =
imagecreatefromjpeg($temp); break;

//png
case "image/x-png" :
case "image/png" : $new_img = imagecreatefrompng($temp);
break;

//gif
case "image/gif" : $new_img = imagecreatefromgif($temp);
break;

//unrecognized format
default : return array(false, "Image #$i must be in jpeg,
gif, or png format.");
}

#no matter what image I put in here .. it always fails here
if(!$new_image) { return array(false, "Error Creating Image #
$i from $temp."); }

//get the original image size
list($w, $h) = getimagesize($temp);

//caluclate scale percentage
$perc = min($max_w/$w, $max_h/$h);
$thumb_p = min($t_w/$w, $t_h/$h);
//calucalte new dimensions
$new_w = round($perc * $w);
$new_h = round($perc * $h);
$thumb_w = round($thumb_p * $w);
$thumb_h = round($thumb_p * $h);

//create a new image with those dimensions
$res_img = imagecreatetruecolor($new_w, $new_h);
$thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
//resize now
imagecopyresampled($res_img, $new_img, 0,0,0,0, $new_w,
$new_h, $w,$h);
imagecopyresampled($thumb_img, $new_img, 0,0,0,0, $thumb_w,
$thumb_h, $w,$h);
//save the images
$new_name = time();
imagejpeg($res_img, "img/".$new_name.".jpg");
imagejpeg($thumb_img, "img/thumbs/".$new_name.".jpg");
//free memory
imagedestroy($res_img);
imagedestroy($thumb_img);
imagedestroy($new_image);

$i++;
}

return array(true,'');
}
?>

The functions: imagecreatefromjpeg(), imagecreatefrompng(), and
imagecreatefromgif() all seem to be returning false. Anyone have any
suggestions?

Thanks,
Chris F.

Feb 5 '07 #1
8 2796

While debugging, you should also print $type, since error might just lie
there.
Feb 5 '07 #2
On Feb 5, 2:44 pm, "ctiggerf" <ctigg...@gmail.comwrote:
I was hopeing someone could help me out here. Been stumped on this
one all day.

This function
1. Checks uploaded files.
2. Creates two resized images from each (a full size, and a
thumbnail).
3. Returns an array with a bool (false if the upload failed), and an
error message.

I am having trouble createing a copy of the image from the functions
imagecreatefrom*

Here is the function:

<?php
function process_images() {
//options
$max_bytes = 65536; // max file size
$max_h = 425; // maximum hieght for images
$max_w = 425; // maximum width for images
$t_h = 125; // max hieght for thumbnails
$t_w = 50; // max width for thumbnails
$max = 2; // max number of images

$i = 1;
while((list($k,$err) = each($_FILES['image']['error'])) and $i<=
$max) {
//check for an error
if($err != UPLOAD_ERR_OK) { return array(false,"There was an
error uploading Image #$i."); }
//TODO: Need better error message for errors here

//get file information
$name = $_FILES['image']['name'][$k];
$type = $_FILES['image']['type'][$k];
$temp = $_FILES['image']['tmp_name'][$k];
$size = $_FILES['image']['size'][$k];

//check that this file is an uploaded file
if(!is_uploaded_file($temp)) { return array(false,"There was
an error uploading Image #$i."); }
//check file size
if($size $max_bytes) { return array(false, "Image #$i
exceeds maximum size"); }

//check type and create a new image based off it
switch($type) {
//jpeg
case "image/pjpeg" :
case "image/jpeg" :
case "image/jpg" : $new_img =
imagecreatefromjpeg($temp); break;

//png
case "image/x-png" :
case "image/png" : $new_img = imagecreatefrompng($temp);
break;

//gif
case "image/gif" : $new_img = imagecreatefromgif($temp);
break;

//unrecognized format
default : return array(false, "Image #$i must be in jpeg,
gif, or png format.");
}

#no matter what image I put in here .. it always fails here
if(!$new_image) { return array(false, "Error Creating Image #
$i from $temp."); }

//get the original image size
list($w, $h) = getimagesize($temp);

//caluclate scale percentage
$perc = min($max_w/$w, $max_h/$h);
$thumb_p = min($t_w/$w, $t_h/$h);
//calucalte new dimensions
$new_w = round($perc * $w);
$new_h = round($perc * $h);
$thumb_w = round($thumb_p * $w);
$thumb_h = round($thumb_p * $h);

//create a new image with those dimensions
$res_img = imagecreatetruecolor($new_w, $new_h);
$thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
//resize now
imagecopyresampled($res_img, $new_img, 0,0,0,0, $new_w,
$new_h, $w,$h);
imagecopyresampled($thumb_img, $new_img, 0,0,0,0, $thumb_w,
$thumb_h, $w,$h);
//save the images
$new_name = time();
imagejpeg($res_img, "img/".$new_name.".jpg");
imagejpeg($thumb_img, "img/thumbs/".$new_name.".jpg");
//free memory
imagedestroy($res_img);
imagedestroy($thumb_img);
imagedestroy($new_image);

$i++;
}

return array(true,'');}

?>

The functions: imagecreatefromjpeg(), imagecreatefrompng(), and
imagecreatefromgif() all seem to be returning false. Anyone have any
suggestions?

Thanks,
Chris F.


I did forget to mention that my php is compiled with GD support
sufficient enough to use the functions and I am using PHP Version
4.3.11 ...

GD Support enabled
GD Version bundled (2.0.28 compatible)
FreeType Support enabled
FreeType Linkage with freetype
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled

Feb 5 '07 #3
On Feb 5, 2:58 pm, "P Pulkkinen"
<perttu.POISTATAMA.pulkki...@POISTATAMA.elisanet.f iwrote:
While debugging, you should also print $type, since error might just lie
there.
I went ahead and added that debugging info and the type seems to be
reporting back correctly for all the types I tried.

Feb 5 '07 #4

"ctiggerf" <ct******@gmail.comkirjoitti
viestissä:11**********************@q2g2000cwa.goog legroups.com...
On Feb 5, 2:58 pm, "P Pulkkinen"
<perttu.POISTATAMA.pulkki...@POISTATAMA.elisanet.f iwrote:
>While debugging, you should also print $type, since error might just lie
there.
I went ahead and added that debugging info and the type seems to be
reporting back correctly for all the types I tried.
What's your error level reporting level, since imagecreatefromsomething
functions echo error message when they fail.
Feb 5 '07 #5
What's your error level reporting level, since imagecreatefromsomething
functions echo error message when they fail.
you could put to the beginning of script:

error_reporting(E_ALL);
Feb 5 '07 #6
On Feb 5, 3:37 pm, "P Pulkkinen"
<perttu.POISTATAMA.pulkki...@POISTATAMA.elisanet.f iwrote:
What's your error level reporting level, since imagecreatefromsomething
functions echo error message when they fail.

you could put to the beginning of script:

error_reporting(E_ALL);
Thanks for your quick replies.
I turned the error reporting on, (it wasn't set high enough to see the
notices) and it is reporting:

Notice: Undefined variable: new_image in /home/content/o/t/b/
otbadmin356/html/order.php on line 365

Which corresponds to the line after the switch statement:

if(!$new_image) { return array(false, "Error Creating Image #$i from
$temp - $type."); }

So I put some echos in to see if I was ever getting inside the switch
statement, and I am. However I do not see any output from the
imagecreatefrom functions.

Feb 5 '07 #7
Rik
ctiggerf <ct******@gmail.comwrote:
Notice: Undefined variable: new_image in /home/content/o/t/b/
otbadmin356/html/order.php on line 365
$new_img != $new_image

--
Rik Wasmus
Feb 5 '07 #8
On Feb 5, 4:40 pm, Rik <luiheidsgoe...@hotmail.comwrote:
ctiggerf <ctigg...@gmail.comwrote:
Notice: Undefined variable: new_image in /home/content/o/t/b/
otbadmin356/html/order.php on line 365

$new_img != $new_image

--
Rik Wasmus
DOH!!

Nothing like spending a whole day looking for a typo. I love
programming.

Thanks for the help, it works great now.
--Chris

Feb 5 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
0
by: Satish Appasani | last post by:
Hi: I have a ASP.NET form with Web layout which I've achieved using panels. In one of the tab I have a File control to upload Images. When I put a file in the file control and move to another...
25
by: moondaddy | last post by:
I have an application where users need to upload images and in my web.config file I have a setting like this: <httpRuntime maxRequestLength="512" /> Which restricts image larger than 500k from...
1
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a...
35
by: Stan Sainte-Rose | last post by:
Hi, What is the better way to save image into a database ? Just save the path into a field or save the image itself ? I have 20 000 images (~ 10/12 Ko per image ) to save. Stan
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
14
by: =?Utf-8?B?U2FtdWVs?= | last post by:
Hi, I have a web app that allows others to upload files, and the problem is that if I allow users to upload image files, fake image can be uploaded and cause XSS issues. In the app, I do...
1
by: RYKLOU | last post by:
I am kinda new to php, but i do know what i am doing kinda, but i came across this error when i am trying to upload a file to my website. Fatal error: Allowed memory size of 8388608 bytes...
4
by: ghjk | last post by:
I wan to add some data using php page to mysql db. But I got the error saying "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.