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;
}