Mankind1023
Mankind1023

Reputation: 7732

php simple image resize after upload

I looked around for a while and found some very confusing and complicated stuff that I couldn't get to work.

I'm using chronoforms with joomla to make a form with an upload file script, and it uploads the image to the server with no problems, so far so good.

I need to take the uploaded image and resize it, better yet, is there a way to resize the image before uploading it to the server?

Thanks.

Upvotes: 5

Views: 28172

Answers (5)

Jarrod
Jarrod

Reputation: 9465

Here is a simple resize library I created and can be found on here on Github.

Will work with any framework.

An example of how to use the library:

// Include PHP Image Magician library
require_once('php_image_magician.php');

// Open JPG image
$magicianObj = new imageLib('racecar.jpg');

// Resize to best fit then crop (check out the other options)
$magicianObj -> resizeImage(100, 200, 'crop');

// Save resized image as a PNG (or jpg, bmp, etc)
$magicianObj -> saveImage('racecar_small.png');

Upvotes: 0

Herr
Herr

Reputation: 2725

[This example] is what you are looking for imagecopyresampled.

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

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

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

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

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

Upvotes: 0

Nimrod007
Nimrod007

Reputation: 9913

I use this easy 1 function that does it all

check it out :

http://www.nimrodstech.com/php-image-resize/

https://github.com/Nimrod007/PHP_image_resize

Upvotes: 15

Sz.
Sz.

Reputation: 3644

Chronoforms (v4 here) does support this out of the box! (I've seen random traces of this for older versions, too, down to 1.3.)

I can just drag an Image Resize action (from under Utilites) to a desired form event (under On Submit).

NOTE: this is not for client-side resizing. For that you'd need a Javascript form uploader package, which can show a thumbnail before & during the upload. They are usually non-trivial to integrate. (And using those client-side thumbnails also for uploading along with the original image requires some even more advanced -- and accordingly more complicated -- stuff; I'd say it's rarely worth the extra pain, just bite the bullet and generate the thumbnail again on the server, and think about all those poor African kids, who have even tougher lifes than web developers. ;) )

Upvotes: 0

Mike Purcell
Mike Purcell

Reputation: 19999

I have used PHPThumb for a few of my projects and found it easy to work with and has a small resource footprint. You can read the docs for more info, but is pretty easy:

$thumb = PhpThumbFactory::create('/path/to/source/image.png');

$thumb->resize(100, 100);

$thumb->save('/path/where/you/want/resized/image.png');

Upvotes: 1

Related Questions