Jump to content


Photo

Thumbnails with cache.


  • Please log in to reply
4 replies to this topic

#1 sprinkles

sprinkles
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 17 July 2006 - 04:54 PM

I've been using a script that wasn't writtenn by me for a while to create thumbnails of logos on the fly.
This is now taking too long with 30+ logos on a page, so i've decided to implement a cache.
When I execute this script, it creates a thumbnail of the image, displays it in the browser, but does not save it. This means the cache does not work at all. I've also tried using fopen and fwrite, but I can't get the image to save.
The images folder is CHMOD 777.

<?php

header("Content-Type: image/jpeg");

$num = $_GET['num'];

$image_path = "http://www.radioo.net/images/$num.jpg";

if (file_exists($image_path)) {
	
	readfile($image_path);
}else{

   if (isset($_GET['img'])) {

        // Generate a thumbnail of the image
        if (!isset($_GET['iw'])) { $iw = 50; } else { $iw = $_GET['iw']; }
        $iname = $_GET['img'];
        if (ereg('png$',$_GET['img'])) {    $i = imagecreatefrompng($iname); }
        if (ereg('jpg$',$_GET['img'])) {    $i =
imagecreatefromjpeg($iname); }
        if (ereg('gif$',$_GET['img'])) {    $i = imagecreatefromgif($iname); }
        if (imageSX($i) <= $iw) {
            imagejpeg($i);
        } else {
            $i2 =
imagecreatetruecolor($iw,($iw*(imagesy($i)/imagesx($i)))+1);

imagecopyresampled($i2,$i,0,0,0,0,$iw,$iw*(imagesy($i)/imagesx($i))+1,imagesx($i),imagesy($i));


           imagejpeg($i2);
           imagejpeg($i2,$image_path);
        
	
	ImageDestroy($i2);

        }
        exit;
    }


}

?> 

Any help appreciated, thanks.


#2 sprinkles

sprinkles
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 18 July 2006 - 10:29 AM

I've also tried this with other paths as tthe image, like ../images/$num.jpg and /images/$num.jpg but it just refuses to save, there are no errors, it just wont save.

#3 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 18 July 2006 - 11:14 AM

I can't see where the error is with your code, but this function works:

function CreateThumbnail($source, $dest, $maxdim = 100) {

  $orig_size = getimagesize($source);
  if ($orig_size[0] <= $maxdim and $orig_size[1] <= $maxdim) return;

  if ($orig_size[0] > $orig_size[1]) {
    $tnw = $maxdim;
    $tnh = round($maxdim * $orig_size[1] / $orig_size[0]);
  } else {
    $tnh = $maxdim;
    $tnw = round($maxdim * $orig_size[0] / $orig_size[1]);
  }

  $original = imagecreatefromjpeg($source);
  $thumbnail = @imagecreatetruecolor($tnw, $tnh);

  imagecopyresized($thumbnail, $original, 0, 0, 0, 0, $tnw, $tnh, $orig_size[0], $orig_size[1]);

  imagejpeg($thumbnail, $dest, 90);
  imagedestroy($original);
  imagedestroy($thumbnail);

}

Using this - modified to suit your environment - you can create the thumbnail file and simply create an html link to it, rather than have code return the file as a jpeg. Though the way you are doing it should work as well.

Hope this helps.

Sam.

If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.

#4 sprinkles

sprinkles
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 18 July 2006 - 11:34 AM

Thanks, but that code is almost exactly the same, apart from the fact the one I'm using checks the file extension too.

#5 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 18 July 2006 - 01:11 PM

...and of course, this code works.

What I would do with something like this, is try it, check that it works, and then bit by bit convert it back to my own code, to see where my code was going wrong.

I can't see the error but I'm not going to do the work for you. At a quick glance I would probably try saving the file to disk before starting the streaming, but I can't see why it matters.

Good luck.


If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users