[SOLVED] Problem with imagecreatefromjpeg()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mattachoo
Forum Newbie
Posts: 5
Joined: Fri Feb 20, 2009 12:48 pm

[SOLVED] Problem with imagecreatefromjpeg()

Post by Mattachoo »

Hello there! I have a problem here that is bugging the hell out of me and was wondering if anyone could give me some insight. I will try to explain my problem in the greatest amount of detail that I can.

I have written a script for a client that allows him to "add new products" to his webpage. In this form, the user can select a hi-res picture of the product for upload. Upon upload, my script takes this image and makes two copies of the image; one of width 50px, and one of width 183px, for thumbnail purposes.

The client contacted me for this project before owning webspace himself. So, while I was writing the code, I tested it on my own site. I finished writing all the code, and everything worked fine on my end. Now he has bought the webspace, and I have started transferring all the files over to his site so we can get this thing up and running. This is where the problem comes in.

After I transferred the scripts and such over to his site, the image resizing script stopped working. I was able to traack down where my script was failing, and it happens when I try and invoke a imagecreatefromjpeg() function. I have tried contacting the tech support people for the website, but they have been useless to me. Here is the snippet of my code:

Code: Select all

 
<?php
 
$<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> = "http://www.theguyswebsite.com/images/products/";
// The file
$filename = $_POST['fname'];
 
$path_parts = pathinfo($filename);
 
$imageWithExt = $path_parts['basename'];
$extension = $path_parts['extension'];
$imageWithoutExt = basename($imageWithExt, ".".$extension);
//echo "with ".$imageWithExt." without ".$imageWithoutExt."<br>";
        
list($width2, $height2, $type2, $attr2) = getimagesize($filename);
//echo "<h4>width ".$width2." height ".$height2." type ".$type2." attr ".$attr2."</h4>";
 
//do the second thumbnail   
$width_2 = 183;
$num183 = 183/$width2;
$height_2 = $height2*$num183;
//working
 
// Resample
 
$image_p2 = imagecreatetruecolor($width_2, $height_2);
 
////////////////////////////////////////////////////////
//CODE FAILS HERE!!!  DAMNIT!
$image4 = imagecreatefromjpeg($filename);
////////////////////////////////////////////
 
imagecopyresampled($image_p2, $image4, 0, 0, 0, 0, $width_2, $height_2, $width2, $height2);
 
// Output
ob_start();
imagejpeg($image_p2, null, 100);
$thefile2 = ob_get_contents();
ob_end_clean();
 
//the thumbnail name will be the 
//same, only it will have a "_thumb2" added to 
//the end of its filename
$newname2 = $imageWithoutExt.'_thumb2';
 
$destination2 = 'images/products/'.$newname2.'.jpg';
 
if (!$handle2 = fopen($destination2, 'w')) {
    echo 'Cannot Open (' . $destination2 . ')';
} else {
    if (fwrite($handle2, $thefile2) === FALSE) {
        echo 'Cannot write to file (' . $destination2 . ')';
    } else {
        echo 'Thumbnail 2 was sucessfully saved!';
    }
    fclose($handle2);
    fclose($handle);
}
 
?>
 
 
I know my code is sloppy, and my variable names are a bit weird, but this code works I tell ya! Oh, and I switched out the real website name with theguyswebsite.com, so don't mind that.

I know for a fact that this code works. The problem does not lie there. If this snippet won't work, that is because it is a snippet, and not the complete file. The problem doesn't lie with my code, is has to do with the web host.

So, my question for you guys is, what could possible make the function imagecreatefromjpeg() fail? I have a hunch there is some funny business going on in my php.ini file that is causing this problem, but I haven't the slightest idea what it actually is. I also know this isn't a function that the web host blocks because my website, (the one where this code executes just fine and does what it is supposed to do), yeah, my website is hosted through the same company.

If anyone has any information on this or could give me any insight to where to look to see where the problem in (by looking at the information by doing a phpinfo() maybe?) I would greatly appreciate it. I just can't figure this one out. Sorry for the long post, but I didn't want to leave anything out. Again, thank you very much in advance if you help me out!

I almost forgot, I also got this error message one time:

Code: Select all

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 5760 bytes) in /usr/local/psa/home/*******/addproduct.php on line 218"
Thanks again!

-Matt
Last edited by Mattachoo on Fri Feb 20, 2009 2:10 pm, edited 1 time in total.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Problem with imagecreatefromjpeg()

Post by greyhoundcode »

By default I think most hosting packages limit the amount of memory available to 8Mb, and that's something you can adjust in your php.ini file.

You might also run phpinfo() and check if you (or your client) has the GD lib available.
Mattachoo
Forum Newbie
Posts: 5
Joined: Fri Feb 20, 2009 12:48 pm

Re: [SOLVED] Problem with imagecreatefromjpeg()

Post by Mattachoo »

The problem was with the memory_limit value in the php.ini file. Once I increased it from 8M to 16M, the script worked fine. :D
Post Reply