Convert JPEG to a Progressive JPEG in PHP

2020-03-08 06:09发布

问题:

I'm trying to re-save an already created JPEG as a progressive jpg.

Basically, on the front end I have some JS that crops an image, and outputs a base64 image JPEG. Once it is passed to the PHP script, I create it as a regular JPEG file, like so:

$largeTEMP = explode(',', $_POST['large']);
$large = base64_decode($largeTEMP[1]);
file_put_contents('../../images/shows/'.$dirName.'/large.jpg', $large);

I'm wanting this jpg image to be progressive, so I was looking around and found the PHP function imageinterlace; however, I can't seem to get it to work. I've tried various combinations, but I feel like I'm going about this in the wrong way for some reason.

So my question is, how can I take my already generated JPEG and convert it to be progressive using PHP? Or, better yet, convert it to a progressive JPEG before I even save it in the first place.

回答1:

Create image resource with imagecreatefromstring:

$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im === false) {
  die("imagecreatefromstring failed");
}
imageinterlace($im, true);
imagejpeg($im, 'new.jpg');
imagedestroy($im);


回答2:

Try Imagick (ImageMagic Package) as shown in here : http://php.net/manual/en/imagick.setinterlacescheme.php

$image = new Imagick('image.jpg');

$image->thumbnailImage(500, 0);

$image->setInterlaceScheme(Imagick::INTERLACE_PLANE);

$image->writeImage('progressive.jpg');


回答3:

Use imageinterlace.

$src_img = imagecreatefromjpeg('source.jpg');

imageinterlace($src_img, true);

imagejpeg($src_img, 'destination.jpg');

imagedestroy($src_img);


标签: php gd