Forums

This topic is locked

Resize images on the fly instead of saving to disc?

Posted 17 Nov 2010 12:25:07
1
has voted
17 Nov 2010 12:25:07 ted thompson posted:
Hi

I've spent a good many hours trawling the web for some decent PHP scripts to resize and crop images on the fly. Resizing on the fly the img src tag of the thumbnail points to a PHP page which does the resizing/cropping and returns the end result to the browser as opposed to saving the file to disc.

I have purchased the Smart Image Processor PHP 2 as I haven't got the time or the knowledge to rewrite some of my Google findings; I'm just getting to grips with PHP after many years of ASP!

Does anybody know how to resize/crop on the fly with Smart Image Processor PHP 2? From my initial playing around with the extension it appears to just write files to disc unfortunately. Anybody the wiser?

Many Thanks
Ted

Replies

Replied 17 Nov 2010 17:25:42
17 Nov 2010 17:25:42 Patrick Woldberg replied:
The Smart Image Processor 2 PHP can only save the files to hdd.

You have to code it yourself. When using GD2 you can try:
<?php
// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

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

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>


Sample is taken from php.net and does only a resize, hope it will help you. Or just use the Smart Image Processor which is very easy to use and load the new image from hdd.

Reply to this topic