I've got a very strange behaviour when I'm uploading an image to multiple folders. I'm trying to add the uploaded images to 3 different folders:
- /userimg/original (just the original picture)
- /userimg/100x100 (100px width and 100px heigh + do crop)
- /userimg/1000x1000 (max 1000px width and max 1000px height)
My script fills the first folder with all selected images (in $_FILES), but the other folders are not filled at all. If I put the '100x100'-folder first in my dimensions array, then that's the folder that is filled with images while the others aren't. I've been debugging for 2 days now and I'm getting a little bit nervous :P
Here's my script:
<?php
App::uses('Folder', 'Utility');
class UserImage extends CoasterCmsAppModel
{
public $useTable = false;
/** @var array $dimensions Dimensions for photo resizing. */
public $dimensions = array(
array(
'width' => 999999,
'height' => 999999,
'crop' => false,
'autocrop' => false,
'quality' => 100,
'folder' => 'original'
),
array(
'width' => 100,
'height' => 100,
'crop' => true,
'autocrop' => true,
'quality' => 60,
'folder' => '100x100'
),
array(
'width' => 1000,
'height' => 1000,
'crop' => false,
'autocrop' => true,
'quality' => 80,
'folder' => '1000x1000'
)
);
public function uploadImages($images) {// this function is called to process the images. $images = $_FILES
if (!$images || !is_array($images)) {
throw new NotFoundException(
__('Invalid data.')
);
}
App::import('Component', 'ImageCropResize.Image');
$image = new ImageComponent(new ComponentCollection);
$userImgFolder = WWW_ROOT . 'userimg' . DS;
foreach ($this->dimensions as $dimension) {
$userImgDimensionFolder = $userImgFolder . $dimension['folder'] . DS;
$folder = new Folder($userImgDimensionFolder);
if (is_null($folder->path)) {
$folder->create($userImgDimensionFolder, 0755);
}
}
$jsonArray = [];
foreach ($images['name'] as $key => $imageName) {
$imageNameCleaned = basename($imageName);
$imageNameCleaned = str_replace(' ', '-', $imageNameCleaned);
$imageNameCleaned = strtolower($imageNameCleaned);
$imageNameCleaned = time() . '-' . $imageNameCleaned;
foreach ($this->dimensions as $dimension) {
$userImgDimensionFolder = $userImgFolder . $dimension['folder'] . DS;
$imagePath = $userImgDimensionFolder . $imageNameCleaned;
move_uploaded_file($images['tmp_name'][$key], $imagePath);
}
}
}
}
Extra info: here's an example of my $images-array that is loaded into the 'uploadImages' function:
array(
'name' => array(
(int) 0 => '1.jpg',
(int) 1 => '2.jpg',
(int) 2 => '3.jpg',
(int) 3 => '4.jpg',
(int) 4 => '5.jpg',
(int) 5 => '6.jpg',
(int) 6 => '7.JPG',
(int) 7 => '8.jpg'
),
'type' => array(
(int) 0 => 'image/jpeg',
(int) 1 => 'image/jpeg',
(int) 2 => 'image/jpeg',
(int) 3 => 'image/jpeg',
(int) 4 => 'image/jpeg',
(int) 5 => 'image/jpeg',
(int) 6 => 'image/jpeg',
(int) 7 => 'image/jpeg'
),
'tmp_name' => array(
(int) 0 => 'C:\xampp\tmp\phpB877.tmp',
(int) 1 => 'C:\xampp\tmp\phpB897.tmp',
(int) 2 => 'C:\xampp\tmp\phpB898.tmp',
(int) 3 => 'C:\xampp\tmp\phpB8A9.tmp',
(int) 4 => 'C:\xampp\tmp\phpB8AA.tmp',
(int) 5 => 'C:\xampp\tmp\phpB8AB.tmp',
(int) 6 => 'C:\xampp\tmp\phpB8AC.tmp',
(int) 7 => 'C:\xampp\tmp\phpB8AD.tmp'
),
'error' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
(int) 2 => (int) 0,
(int) 3 => (int) 0,
(int) 4 => (int) 0,
(int) 5 => (int) 0,
(int) 6 => (int) 0,
(int) 7 => (int) 0
),
'size' => array(
(int) 0 => (int) 4498964,
(int) 1 => (int) 372372,
(int) 2 => (int) 219612,
(int) 3 => (int) 15807,
(int) 4 => (int) 91903,
(int) 5 => (int) 56157,
(int) 6 => (int) 138041,
(int) 7 => (int) 81819
)
)
Any thoughts to guide me into the right direction? And yes, I do appreciate some extra tips and hints about my PHP code ;)
Edit 1 after feedback by Dorvalla
Here's my new $dimensions array, but this seems not to work:
public $dimensions = array(
0 => array(
'width' => 999999,
'height' => 999999,
'crop' => false,
'autocrop' => false,
'quality' => 100,
'folder' => 'original'
),
1 => array(
'width' => 100,
'height' => 100,
'crop' => true,
'autocrop' => true,
'quality' => 60,
'folder' => '100x100'
),
2 => array(
'width' => 1000,
'height' => 1000,
'crop' => false,
'autocrop' => true,
'quality' => 80,
'folder' => '1000x1000'
)
);