2

I'm trying to allow users to upload an image and save the image as an 'original copy' in the directory

public_html/data/user_images/yyyy/MM/dd/guid.ext 

Next, using GD I create multiple copies of the image at different sizes and save to a another dynamic location in the file system.

public_html/data/user_images/width/yyyy/MM/dd/guid.ext  

I keep coming across the following error which leads me to believe that the file needs to exist already before I can save over a manipulated image using the GD library.

imagejpeg(/data/user_images/200/2013/05/21/9714624e10eed645e822babd0acccf69ac421d59.png): 

failed to open stream: No such file or directory

I get the same error with both relative and absolute paths. I know this directory or file doesn't exist. The code works as expected when saving over the original copy of the uploaded image, I just can't create a new one in a dynamic directory.

Below is the code from my image helper. As parameters it takes the original saved image location, an array of sizes and a file name for the new images. The createDirectories function returns an array containing the absolute and relative dynamically created paths.

public static function create_sizes($image, $sizes, $filename){

    $current_size = GetimageSize($image);
    $urls = array();

    switch(strtolower($current_size['mime'])){
        case 'image/png':
            $image_original = imagecreatefrompng($image);
            break;
        case 'image/jpeg':
            $image_original = imagecreatefromjpeg($image);
            break;
        case 'image/gif':
            $image_original = imagecreatefromgif($image);
            break;
        default: die();
    }



    foreach($sizes as $width){

        $directories = self::createDirectories('user_images/'.$width);

        $aspect_ratio = $current_size[0] / $current_size[1];
        $height = (int) ($width / $aspect_ratio);

        $photoX = ImagesX($image_original);
        $photoY = ImagesY($image_original);

        $image_new = ImageCreateTrueColor($width, $height);

        ImageCopyResampled($image_new, $image_original, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);

        ImageJPEG($image_new, $directories['absolute'].$filename, 100);
        array_push($urls, $directories['relative'].$filename);
    }

    ImageDestroy($image_original);
    ImageDestroy($image_new);

    return $urls;
}

1 Answer 1

3

Have a quick look on my ImageProcessor class. I think it will help you: http://pastebin.com/zNY02N57

Usage: When user uploads a file it's name is in $_FILES['input_name']['tmp_name']. So first of all create empty instance of my class:

$OriginalImage = new ImageProcessor();
//Note that it will automatically detect the file type :)
if(!$OriginalImage->Load($_FILES['input_name']['tmp_name']))
    die('Image is invalid');

Now you have to choose the method of resizing. You can force image to have fixed width or height or to have exact size you choose, but it will be on black background if it does not fit.

Fixed width:
$NewImage = $OriginalImage->ResizeWPreserve($NewWidth)
Fixed height:
$NewImage = $OriginalImage->ResizeHPreserve($NewHeight)
Fixed size:
$NewImage = $OriginalImage->ResizeCanvas($NewWidth, $NewHeight)

Then, to save it, simply:
$NewImage->Save('FileName.jpg');

You can repeat save and resize in a loop like:

$Sizes = array(640, 800, 1024);
foreach($Sizes as $Width)
{
    $OriginalImage->ResizeWPreserve($Width)->Save(sprintf('Images/%s/%d.jpg', 'SomeName', $Width));
}

Class

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, your code has convinced me that what I'm currently doing is correct. At this point imagepng($this->I, $OutFile); in your code is where I'm getting an error in mine. Could it be a permissions issue? I just can't seem to create new directories or files with GD
The directory you are putting the file in must exist.
Yeah, I modified my code and it works perfectly now. I create a directory with the master image of yyyy/MM/dd/ and append the size to the file name. Thanks for your help with this Peku

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.