How to move image to new folder?

As the title says. How to move / rename an image to a new folder? I still have it, and the new image is resized / cropped, but it does not move to the "new /" folder:

$in_filename = '4csrWqu9ngv.jpg'; list($width, $height) = getimagesize($in_filename); $offset_x = 0; $offset_y = 0; $new_height = $height - 65; $new_width = $width; $image = imagecreatefromjpeg($in_filename); $new_image = imagecreatetruecolor($new_width, $new_height); imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height); header('Content-Type: image/jpeg'); imagejpeg($new_image); $move_new = imagejpeg($new_image); rename($move_new, 'new/' . $move_new); 

As always, any help is appreciated :)

+4
source share
3 answers

You had few errors in your code. The imagejpeg output is logical, so your renaming always fails. You also never saved the modified image. You must use the 2nd parameter of imagejpeg and provide the correct file name for the new image. Also, make sure the new directory exists, otherwise the renaming will fail.

Fixed Code:

 $in_filename = '4csrWqu9ngv.jpg'; list($width, $height) = getimagesize($in_filename); $offset_x = 0; $offset_y = 0; $new_height = $height - 65; $new_width = $width; $image = imagecreatefromjpeg($in_filename); $new_image = imagecreatetruecolor($new_width, $new_height); imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height); /* Uncomment in case you want it also outputted header('Content-Type: image/jpeg'); imagejpeg($new_image); */ imagejpeg($new_image, $in_filename); rename($in_filename, 'new/' . $in_filename); 
+5
source

maybe this should help you.

+2
source

Is there a "new" folder? If not, you need to create it first using mkdir .

0
source

All Articles