writeImage() in different folder with the same name

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
cuba
Posts: 2
Joined: 2013-10-14T11:00:24-07:00
Authentication code: 6789

writeImage() in different folder with the same name

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

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

Post 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);
}
?>
cuba
Posts: 2
Joined: 2013-10-14T11:00:24-07:00
Authentication code: 6789

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

Post 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.
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

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

Post by mkoppanen »

Does $newPath exist? If not, you have to create it first.
Mikko Koppanen
My blog: http://valokuva.org
Post Reply