Jump to content

Recommended Posts

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.

[code=php:0]
<?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;
    }


}

?>
[/code]

Any help appreciated, thanks.
Link to comment
https://forums.phpfreaks.com/topic/14860-thumbnails-with-cache/
Share on other sites

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

[code]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);

}[/code]

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.
Link to comment
https://forums.phpfreaks.com/topic/14860-thumbnails-with-cache/#findComment-59858
Share on other sites

...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.

Link to comment
https://forums.phpfreaks.com/topic/14860-thumbnails-with-cache/#findComment-59877
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.