Page 1 of 1

writeImage() in different folder with the same name

Posted: 2013-10-14T11:08:58-07:00
by cuba
Hi Guys, I have a problem and spend all day to solve it, but without any results.

I try to get all images from the folder and save it to another folder with the same name.

Variable $filename store the path to file with the name, so I try to merge variables to add new path, but its not working. If I use $filename in $im->writeImageFile($newPath); original images replace with new one.

Code: Select all

foreach (glob("images/*.jpg") as $filename) {
    echo "$filename size " . filesize($filename) . "<br>";

    /* Create new imagick object */
    $im = new imagick($filename);

    /* real size */
    $size = $im->getImageWidth();
    echo "$size <br>";

    /* size after resizing */
    $im->thumbnailImage(120,0);
    $sizeNew = $im->getImageWidth();
    echo "$sizeNew <br>";

    /* make and see new path */
    $newF= "new/" ;
   $newPath = $newF.$filename;
   echo $newPath;

    /* write image to new path */
  $im->writeImageFile($newPath);
}
What should i do to save files in new folder?

All permisions sets to 777, folder is exist, and imagick can save images to this folder.

Re: writeImage() in different folder with the same name

Posted: 2013-10-14T12:52:17-07:00
by Bonzo
So according to your code you have a folder containing the code and an images folder containing your jpg files.

Main folder with code > images folder with jpgs

You are trying to save you modified images to a folder called new and the path to this is:

main folder with code > new folder > images folder > modified jpg image

I would guess this is not what you want. You will need to do something with $filename to extract what you want - just the filename without the images part?

Try this; it saves the new image into a folder called new
Main folder > new modified jpg image

Code: Select all

<?php
foreach (glob("images/*.jpg") as $filename) {
    echo "$filename size " . filesize($filename) . "<br>";

    /* Create new imagick object */
    $im = new imagick($filename);

    /* real size */
    $size = $im->getImageWidth();
    echo "$size <br>";

    /* size after resizing */
    $im->thumbnailImage(120,0);
    $sizeNew = $im->getImageWidth();
    echo "$sizeNew <br>";

    /* make and see new path */
	$parts = explode('/', $filename );
    $newF= "new/" ;
   $newPath = $newF.$parts[1];
   echo $newPath;

    /* write image to new path */
  $im->writeImage($newPath);
}
?>

Re: writeImage() in different folder with the same name

Posted: 2013-10-14T17:16:15-07:00
by cuba
Now it's working well, big thanks for you for right direction, i will see more about "explode" to understand how it works.
You are trying to save you modified images to a folder called new and the path to this is:

main folder with code > new folder > images folder > modified jpg image

I would guess this is not what you want. You will need to do something with $filename to extract what you want - just the filename without the images part?
It is not very important where to save files, but really i try to copy files to "new/images/*.jpg" and I really confused about the $newPath isn't good for writeImageFile(), because when I echo $newPath , i see that it's what i need.
Maybe the problem that $newPath become a String and not more a path?.. I will think on it.

Re: writeImage() in different folder with the same name

Posted: 2013-10-31T11:21:13-07:00
by mkoppanen
Does $newPath exist? If not, you have to create it first.