sprinkles Posted July 17, 2006 Share Posted July 17, 2006 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]<?phpheader("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. Quote Link to comment https://forums.phpfreaks.com/topic/14860-thumbnails-with-cache/ Share on other sites More sharing options...
sprinkles Posted July 18, 2006 Author Share Posted July 18, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14860-thumbnails-with-cache/#findComment-59853 Share on other sites More sharing options...
SammyP Posted July 18, 2006 Share Posted July 18, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14860-thumbnails-with-cache/#findComment-59858 Share on other sites More sharing options...
sprinkles Posted July 18, 2006 Author Share Posted July 18, 2006 Thanks, but that code is almost exactly the same, apart from the fact the one I'm using checks the file extension too. Quote Link to comment https://forums.phpfreaks.com/topic/14860-thumbnails-with-cache/#findComment-59864 Share on other sites More sharing options...
SammyP Posted July 18, 2006 Share Posted July 18, 2006 ...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. Quote Link to comment https://forums.phpfreaks.com/topic/14860-thumbnails-with-cache/#findComment-59877 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.