1

Is it possible to have png images on my server but have a php script that converts them to jpg (and compresses them) and caches them when viewed?

Skyfish
  • 189
  • 1
  • 1
  • 7

2 Answers2

3

You can use this script for convert PNG image into JPEG image.

$input_file = "test.png";
$output_file = "test.jpg";

$input = imagecreatefrompng($input_file);
list($width, $height) = getimagesize($input_file);
$output = imagecreatetruecolor($width, $height);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);

You can go through this @Alexandre Jasmin Answer.

Community
  • 1
  • 1
VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63
  • It appears this doesn't cache the image, ie. they are regenerated each time the page is viewed (would this be a problem for speed?), what would I have to add to this script to cache? So it only generates a new jpg if the png has changed from the last time a jpg was generated. – Skyfish Apr 12 '15 at 06:18
1

yes it is possible, you can google it using phrase "png convert to jpg php"

Use PHP to convert PNG to JPG with compression?

Community
  • 1
  • 1
LPodolski
  • 2,888
  • 4
  • 21
  • 24