tags:

views:

396

answers:

4

I have a web page that displays images that I don't know their size in advance. I was trying to use the GD functions to make the script resize and crop the images from me " Just before they are displayed.. I don't need caches" but I failed. I need a script that I can call like this

<img src="display.php?src=blablabla&height=100&width=200" ?>

or even by calculating the width and height of css to preserve the proportions and make the image touch the box from inside like

<img src="blabla.jpg" style="height:<?php echo $height; ?>; width:<?php echo width; ?>" />

I don't need any sort of caching. How can I do that ?

A: 

You are looking for TimThumb (Demo | Source Code):

Simply copy the source code into a new document called ‘timthumb.php’, place it in a folder on your site (ex: /scripts/) and call the image like this:

<img src="/scripts/timthumb.php?src=/images/whatever.jpg&h=150&w=150&zc=1" alt="">
Alix Axel
TimThumb is my problem. I was trying to debug a code that has timthumb and it drove me crazy that i decided to write my own code !
ta.abouzeid
@ta.abouzeid: TimThumb is pretty easy to understand, what are trying to do / isn't working?
Alix Axel
@Alix Axel: Check this theme, http://www.web2feel.com/scarlett-theme/it uses timthumb in the slide.php file and itsn't working for all the people out there
ta.abouzeid
A: 

OR phpthumb http://phpthumb.sourceforge.net/

Demo is available at: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php

Jay Zeng
Does phpthumb support caching?
winsmith
Looks like it does support caching, per its intro "Thumbnails can be cached for less server load. Mulitple sizes of any source image can be cached seperately. Thumbnails are automatically updated when (local) source image is modified."
Jay Zeng
+3  A: 

You'll need to use the first style. Because this would be happening server-side, you can't check the CSS to get the desired size.

You just need to use the GD functions to open the appropriate file, use imagecopyresampled() to resize it, and then output to the buffer using imagejpeg. Don't forget to set the right headers:

header('Content-type: image/jpeg');
nickf
Besides GD, you can also consider using Imagemagick
Jay Zeng
+3  A: 

WideImage rlz! :)

The resize's like that:

header('Content-type: image/jpeg');

echo WideImage::load('image.jpg')->resize(200, 100)->asString('jpg', 80);
// image.jpg resized at 200x100 with 80% of quality
TiuTalk
Wow that worked !! Thanx man .. But what if I wanna crop it slightly first so that it can touch the box (200,100) from inside .. I mean I want every pic to be 200 x 100 but with minimal loss of details by cropping.
ta.abouzeid
So you can do this:echo WideImage::load('image.jpg')->resize(200, 100, 'outside')->crop->('50% - 100', '50% - 50', 200, 100)->asString('jpg', 80);
TiuTalk