GD Graphics Library and Dreamhost

There hasn’t been any discussion on this topic in like 3 years… so maybe I’m just oblivious to something obvious. BUT, I can’t figure out how to get GD PHP commands to work on my Dreamhosted site! I’m just trying to run a very basic image upload+resize script, so I don’t think there are any out of the ordinary commands I’m using. Here are a few of them out of the script:



Help would be greatly appreciated!

What kind of errors are you running into? I haven’t used all of those, but I have been using the GD library quite a bit and haven’t hit any snags.

Check that they all exist with function_exists() and go from there.

Check out Gordaen’s Knowledge, the blog, and the MR2 page.

I’m getting this error:

Warning: copy(images/original.jpg) [function.copy]: failed to open stream: No such file or directory in /home/.bello/rzoopscom/ on line 68
ERROR: Unable to upload image.

Here is the actual block of code I’m using… it’s just a simple image uploader and resizer.

<?php $idir = "images/"; // Path To Images Directory $tdir = "images/thumbs/"; // Path To Thumbnails Directory $twidth = "125"; // Maximum Width For Thumbnail Images $theight = "100"; // Maximum Height For Thumbnail Images if (!isset($_GET['subpage'])) { // Image Upload Form Below ?> File:

<? } else if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') { // Uploading/Resizing Script $url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location print 'Image uploaded successfully. '; // Was Able To Successfully Upload Image $simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From $currwidth = imagesx($simg); // Current Image Width $currheight = imagesy($simg); // Current Image Height if ($currheight > $currwidth) { // If Height Is Greater Than Width $zoom = $twidth / $currheight; // Length Ratio For Width $newheight = $theight; // Height Is Equal To Max Height $newwidth = $currwidth * $zoom; // Creates The New Width } else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) $zoom = $twidth / $currwidth; // Length Ratio For Height $newwidth = $twidth; // Width Is Equal To Max Width $newheight = $currheight * $zoom; // Creates The New Height } $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail imagetruecolortopalette($simg, false, 256); // Create New Color Pallete $palsize = ImageColorsTotal($simg); for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use } imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "$tdir" . $url); // Saving The Image imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image print 'Image thumbnail created successfully.'; // Resize successful } else { print 'ERROR: Unable to upload image.'; // Error Message If Upload Failed } } else { print 'ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong print $file_ext; // Show The Invalid File's Extention print '.'; } } ?> -------------------------------------------------------------------

That’s not a GD error. It’s a directory/open_basedir error.

First, I’d echo out your destination path to make sure your vars are populating correctly.

Second, make sure you’re using “MAXFILESIZE” in your submit form right before your image and make sure the size is set high enough. If it’s too low, your image will come through with a zero file size.

Three, make sure your directory owner is correct, especially if you are attempting to overwrite existing files.

If none of the above works, ask support to check the /tmp file on your server. They always pretend like there’s nothing wrong, but there is a knowledge base article buried in there about funky open_basedir issues. I don’t know if they reboot the server, restart Apache, or what, but usually soon after I contact them the upload script starts magically working with no changes on my part.

I hope that helps you out!

Angela Gann
CrimsonDryad Web Design Services
Web Design, Custom Software Development

That is a very good point, Crimsondryad. I finally abandoned the use of my existing mod_php installations because of the unpredictability of the way open_basedir works. I think it’s just buggy, as it does not work reliably on DH.


Dreamhost has disabled openbase_dir functions because of hacking concerns. Use the CURL library functions to import a local copy of a file for processing instead of specifying the URL.

That is, simply stated, not true.

From the DH Wiki:

open_basedir restrictions prevent any of PHP’s file opening commands from working on any files outside of /home/youruser. Files BELOW that directory WILL WORK:

/home/youruser/phpstuff/some/more/dirs/ IS OK! PHP CAN OPEN IT!

NOTE: There have been some strange bugs that appeared with respect to open_basedir. Sometimes files that ARE in your home directory trigger an open_basedir error when they really shouln’t. Notify support when this happens.

This is only relevant in the mod_php environment, and I have seen them behave in an unpredictable manner, as indicated above.

Or, simply use an absolute or relative path iinstead of a url (not related to openbase_dir , rather Allow url fopen)