Reputation: 724
I've set up jCrop so that users on my site can crop and save (download) images. I'm near the end now however I've hit a wall. I've got the code set up (see below) and when I comment out the header()
line everything works as intended, the cropped image is saved to the server and I can download it from there.
The problem is that when I include the header and broken image is returned and I'm really not sure why. If anyone could help I would be most grateful
<?php
//$_POST['src'] = http://example.com/example.jpg
$image_url = $_POST['src'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$targ_w = $_POST['w']; $targ_h = $_POST['h'];
$image = imagecreatefromstring( $data );
$dst_r = imagecreatetruecolor( $targ_w, $targ_h );
if ( !$image )
exit( 'No valid image' );
$path = '/home/hostname/public_html/'.time().'-image.jpg';
imagecopyresampled( $dst_r, $image, 0, 0, $_POST['x'], $_POST['y'], $targ_w, $targ_h, $_POST['w'], $_POST['h'] );
//header( 'Content-Type: image/jpeg' );
imagejpeg( $dst_r, $path, 100 );
imagedestroy( $dst_r );
//unlink( $path );
exit;
?>
Currently as the code stands the image is saved in the root of the site. If I uncomment the unlink()
the image is deleted from the directory (which is intended). If I uncomment the header()
line then a page is returned containing a broken image with the src matching the page url.
Thank you.
Upvotes: 0
Views: 71
Reputation: 444
Like the documentation specifies, the second argument of imagejpeg
should be NULL
if you want to output the image directly.
Try changing imagejpeg( $dst_r, $path, 100 );
to imagejpeg( $dst_r, NULL, 100 );
.
Edit: this also saves you from using unlink
, since no file is made.
Upvotes: 1