简体   繁体   中英

Outputting to a file, not using headers for a jpeg using a PHP

Have come up with the following code that resizes an image, however it won't output the file to picture2.jpg and just displays on the screen instead.

$src_img = imagecreatefromjpeg('picture.jpg');
$srcsize = getimagesize('picture.jpg');

$dest_x = 500;
$dest_y = (500 / $srcsize[0]) * $srcsize[1];

$dst_img = imagecreatetruecolor($dest_x, $dest_y);

//  Resize image
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $srcsize[0], $srcsize[1]);


//  Output image
header("content-type: image/png");
imagepng($dst_img);

$myFile = "picture2.jpg";
$fh = fopen($myFile, 'w');
fwrite($fh, $dst_img);
fclose($fh);

//  Deletes images
imagedestroy($src_img);

Am lost as to why it won't do so as thought you could write to a file using fwrite.

To save your image to a file use the second argument to imagepng/imagejpeg

 imagejpeg($dst_img, "picture2.jpg");

or

 imagepng($dst_img, "picture2.png");

Omitting this argument just sends the file to the output buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM