Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP crop and resize image on the fly

Tags:

html

css

php

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 ?

like image 477
taabouzeid Avatar asked Jan 19 '26 06:01

taabouzeid


2 Answers

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
like image 112
Thiago Belem Avatar answered Jan 20 '26 20:01

Thiago Belem


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');
like image 30
nickf Avatar answered Jan 20 '26 20:01

nickf