Force image renaming to start at 1 and increment

See all posts Reply

Force image renaming to start at 1 and increment new!
by Jason, 16 years, 11 months ago
I'm working on a form that submits 5 images, resizes and renames them...

I need the images to follow this naming convention: image_1.JPG | image_2.JPG | image_3.JPG | image_4.JPG | image_5.JPG

If i set the $handle->file_new_name_body = 'image'; the first image is called image.jpg then, the second image is image_1.jpg and so on...

Can someone please advise how I might be able to do this?Reply
Re: Force image renaming to start at 1 and increment new!
by colin, 16 years, 11 months ago
Why not explicitely set the names of the five images, one by one?

For instance:
$handle->file_new_name_body = 'image_1';
....
$handle->file_new_name_body = 'image_2';
....
$handle->file_new_name_body = 'image_3';
....
$handle->file_new_name_body = 'image_4';
....
$handle->file_new_name_body = 'image_5';
Reply
Re: Force image renaming to start at 1 and increment new!
by Jason, 16 years, 11 months ago
It will only take the last instance of that handle ($handle->file_new_name_body = 'image_5';)

the resulting images are:
image_5.JPG
image_5_1.JPG
image_5_2.JPG
image_5_3.JPG
image_5_4.JPG
Reply
Re: Force image renaming to start at 1 and increment new!
by Jason, 16 years, 11 months ago
I thought i'd elaborate on my first post to make it easier to understand what i'm trying to achieve...

The five images are passed from a form as an array to upload.php.
$files = array();
foreach ($_FILES['image'] as $k => $l) {
  foreach ($l as $i => $v) {
    if (!array_key_exists($i, $files)) 
      $files[$i] = array();
    $files[$i][$k] = $v;
  }
}
foreach ($files as $file) {
  $handle = new Upload($file);
  if ($handle->uploaded) {
    $handle->file_new_name_body = 'image';
    $handle->file_overwrite = true;
    $handle->file_auto_rename = false;
    $handle->file_new_name_ext = 'JPG';
    $handle->image_resize = true;
    $handle->image_x = 280;
    $handle->image_ratio_y = true;
    $handle->jpeg_quality = 60;
  }
}
The form is submitted and the images need to be resized & renamed to image_1.JPG => image_5.JPG and need to overwrite the existing 5 images in the destination directory... (which are also named image_1.JPG => image_5.JPG )

Hope this clarifies what i'm trying to achieve.

Thanks in advance!Reply
Re: Force image renaming to start at 1 and increment new!
by Jason, 16 years, 11 months ago
OK... a bit of thinking and i've fixed the problem!

I defined a variable:
$count=1

then modified the handle:
$handle->file_new_name_body = 'image_'.$count++;

and made sure the following two handles were set:
$handle->file_overwrite = true;
$handle->file_auto_rename = false;

Hopefully this will help someone out there trying to do the same.

Thanks again for a fantastic class!Reply
Re: Force image renaming to start at 1 and increment new!
by colin, 16 years, 11 months ago
Yes, that's the solution I would have suggested. Just force the name of the picture for each one of them.

Glad you like the class :)Reply