Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JPEG to a Progressive JPEG in PHP

Tags:

php

gd

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.

like image 509
Kyle Hawk Avatar asked Jan 19 '17 02:01

Kyle Hawk


3 Answers

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);
like image 72
Ruslan Osmanov Avatar answered Oct 11 '22 01:10

Ruslan Osmanov


Use imageinterlace.

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

imageinterlace($src_img, true);

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

imagedestroy($src_img);
like image 30
Melvin Wong Avatar answered Oct 11 '22 00:10

Melvin Wong


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');
like image 33
Zoedia Avatar answered Oct 11 '22 01:10

Zoedia