1

Good day All

I have been search the forums and I truly hope that this is not a duplicate post because I cannot find the answer to this.

I have a system that is saving High Quality .png images into a database. I do not have control over this portion of the system.

I need to display the images on a webpage. I have this working but the images are 500k at the moment and I load up between 4 - 20 images at a time. Obviously this isn't just going to clean out my bandwidth but it's going to take a very long time to load.

I was wondering if it is possible to convert the PNG output to a smaller JPEG?

Here is the code I have managed to get working with the PNG's.

This is saved as getImageByPkey.php

    <?php
    $pkey = $_GET['pkey'];
    $cam = $_GET['camera'];
    include_once('database.php');
  $sql = "select 
       pic
    from
       $cam
where
    pkey = $pkey";
  $result = mysql_query("$sql");
  $row = mysql_fetch_assoc($result);
  mysql_close($link);

  $image = $row['pic'];

  header("Content-type: image/png");
  echo $image;
 ?>

I use this function to build the images based on an array of Primary Keys numbers.

function createImageList($pkey){    //Create image list from array of primary keys

    $key = count($pkey);
    $key = $key - 1;
    for ($x=$key;$x>=1;$x--){
        echo '<div>';
        echo '<img src="getImageByPkey.php?camera='.$pkey[0].'&pkey='.$pkey[$x].'"><br>';
        $timeDB = getPkeyTimeValue($pkey[0], $pkey[$x]);
        $time = correctedTime($timeDB);
        echo $time;
        ';
    }
}

This code works to display the PNG's just fine. I just need to somehow output JPEGS rather than PNG's.

Please let me know if I have been unclear about anything and please let me know if this has been covered elsewhere.

I appreciate any advise on this.

Thank you

WillemOost
  • 11
  • 1
  • For image resizing you can look at the GD library: http://ie1.php.net/manual/en/book.image.php. Or have a look at Imagine which is a nice library for image processing with GD/ImageMagick (requires at least 5.3): http://imagine.readthedocs.org/en/latest/ – Darragh Enright Feb 24 '14 at 19:59
  • Your best bet, I'd say, would be to check if [GD](http://es1.php.net/manual/en/ref.image.php) is installed. If it is, you probably should create a cache on disk of already reduced images so your code does not have to generate the smaller versions each time. – delCano Feb 24 '14 at 20:00
  • A quick Googling reveals this: http://stackoverflow.com/questions/1201798/use-php-to-convert-png-to-jpg-with-compression – MonkeyZeus Feb 24 '14 at 20:00
  • Just curious, are you going to be performing this conversion every single time the web page is loaded? Or will this be a one time thing per image because you will take it from the DB, convert to JPG, and save it to a directory? – MonkeyZeus Feb 24 '14 at 20:02
  • Also, add a check in your code to see if the smaller images exist (using `file_exists()`), and if they don't, create them. Obviously they won't exist at the start, so your system will have to create them. Then it will load the smaller images each time the page is loaded, saving processing. Or you could also save them in the database and check if they exist at during lookup. Either way... :) – timgavin Feb 24 '14 at 20:03
  • Thanks Darragh, Thanks for the link @MonkeyZeus but that just seems to return a broken link for me. I had already tried it. I am not sure why. I have tried imagick and have the same issue. The webserver is my own so I can install anything I need. My idea was to just display it as there will be very few users on at any given time. I did install imagick but maybe I missed the GD library so that might be why I get the broken image. – WillemOost Feb 24 '14 at 20:18
  • Works for me, try copy+pasting this `http://stackoverflow.com/questions/1201798/use-php-to-convert-png-to-jpg-with-compression` – MonkeyZeus Feb 24 '14 at 20:24
  • Thanks for the ideas on caching locally @delCano and Tim unfortunately though I always display the latest images and they will be updating every 2-3minutes so either way there is going to be server load at least half the time anyone connects. I do think I will do it anyway to save every little bit I can. – WillemOost Feb 24 '14 at 20:26
  • @MonkeyZeus no I meant I tried that Function in that link and I just get a borken image returned. I had already found that particular post. – WillemOost Feb 24 '14 at 20:37
  • I'd be concerned about creating a jpeg because the PNG files may have transparency, which will be lost when converting to jpeg. Can you create a smaller PNG upon upload? Or, instead of creating a jpeg, just create a smaller PNG anyway? – timgavin Feb 24 '14 at 23:23
  • @Tim There is no transparency in the image. They are images from a camera. I have no control over the images going into the database. I will happily display a reduced quality png if you know how. – WillemOost Feb 25 '14 at 04:40
  • @WillemOost that doesn't make sense to me. What camera saves PNG files? Regardless, use `imagecreatefrompng()` to create a smaller PNG image from the original. http://us3.php.net/imagecreatefrompng – timgavin Feb 25 '14 at 05:41
  • @Tim Thank you but that is exactly where I get the broken image link. I will see it must have something to do with php-gd. I will make sure it's installed and then try again with imagecreatefrompng(). – WillemOost Feb 25 '14 at 08:25

0 Answers0