How to save cropped image to a directory using Jcrop using PHP

I can crop my images and click on crop. but then something goes wrong, because I don’t know exactly how to save this image.

I am using imagecreatefromjpeg from php. this code is as follows:

<?php SESSION_start(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $targ_w = 200; $targ_h = 400; $jpeg_quality = 90; $src = $_SESSION['target_path']; $img_r = imagecreatefromjpeg($src); $dst_r = ImageCreateTrueColor( $targ_w, $targ_h ); imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], $targ_w,$targ_h,$_POST['w'],$_POST['h']); //header('Content-type: image/jpeg'); imagejpeg($dst_r, 'uploads/cropped/' . 'filename'); exit; } ?> 

My PHP code to save the original image is as follows:

  <?php session_start(); $target = "uploads/"; $target = $target . basename( $_FILES['filename']['name']) ; $_SESSION['target_path'] = $target; $ok=1; if(move_uploaded_file($_FILES['filename']['tmp_name'], $target)) { echo "De afbeelding *". basename( $_FILES['filename']['name']). "* is geupload naar de map 'uploads'"; } else { echo "Sorry, er is een probleem met het uploaden van de afbeelding."; } ?> 

How to save the cropped image? and is there a way to save the cropped image by overwriting the original to get only the cropped image?

early.

EDIT: I can save 1 photo now. I edited my code: imagejpeg ($ dst_r, 'uploads / cropped /'. $ filename.'boek.jpg '); But I have to create a function that can save multiple files with each other. and possibly overwrite the original image, which is saved in 'uploads /'

+8
php image folder save jcrop
source share
1 answer

I am going to answer a question that now seems to be a question:

But I have to create a function that can save multiple files with each other name. and possibly overwrite the original image, which is saved in 'Adds /'

An easy way to do this is to add a timestamp in the file name, so every time the operation is performed, you get a new file name. Doing something according to:

 imagejpeg($dst_r, 'uploads/cropped/' . $filename . time() . 'boek.jpg'); 

Saves a new image every time.

If you want to overwrite the original, just assign the correct path

 imagejpeg($dst_r, 'uploads/' . $filename .'.jpg'); 

Edit:. It seems that the problem is getting the correct file name without hard coding "boek.jpg".

When saving the image in the source code, the string filename

 imagejpeg($dst_r, 'uploads/cropped/' . 'filename'); 

What you want to do is print the file name from $src . To do this, simply use the basename function (documentation here ), so you will have:

 imagejpeg($dst_r, 'uploads/cropped/' . basename($src)); // basename will already contain the extension 
+7
source share

All Articles