Resize image after upload - php

I want images to resize after upload in 4 different formats. If i resize it to best fit(i.e aspect ratio) some images come too small if height or width is too large than the other and if i resize it to fixed size then images get skewed. So what is the best way to resize a image. I am currently doing this using via imagemagik thumbnailImage() but i think it's a general problem. What are sites like google or facebook doing. what is the best thing to do in that case

You can use resize functionality for resize image in different size during upload image.
For example:
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($_FILES['uploaded_image']['tmp_name']);
$image->resizeToWidth(300);
$image->resizeToHeight(200);
$image->save('resizeImage.jpg'
Similarly, you can save image in different size.
For more in detail you can find here:
http://sanjeevkumarjha.com.np/how-to-resize-and-crop-image/

You can also use ImageWorkshop: http://phpimageworkshop.com/doc/17/resizing.html
$layer = new ImageWorkshop(array("fileObject" => $_FILES["uploadedImage"]));
$layer->resizeInPixel(200, 150, true); // Conserve proportion !
$layer->save(__DIR__."/web/uploads/2012", "thumb.png", true, null, 95);
You will have a resized picture of 200px/150px with conserved proportion !

Related

PHP Resize Image to a specified size

Is it possible to resize an image in PHP, to a specified size in Kilobytes, with large images?
Example: IMGonline.com.ua
You can absolutely resize to a certain size using resize so if you know a certain number of pixels will be within that size you could set a pixel size. I've used 100x100 for reference.
$resize = new ResizeImage('image.png');
$resize->resizeTo(100, 100, 'exact');
$resize->saveImage('/resized/image.png');
Using this theory you could take the same approach and if an image is already that size or below you could resize it.
if ($file_size < 5000) {
$resize = new ResizeImage('image.png');
$resize->resizeTo(100, 100, 'exact');
$resize->saveImage('/resized/image.png');
} else {
// Error
}
So you could run it like this
// Upload Image
// Resize image
$resize = new ResizeImage('image.png');
$resize->resizeTo(100, 100, 'exact');
$resize->saveImage('/resized/image.png');
// Save image
// Open saved image
// Check image size
// Confirm or resize progressively smaller.
Hopefully the resize property is of some use. You've provided no code in your question but I think the best approach would be to check the size and save if it's fine then if not you would resize again and check again. Obviously you'd have to do this until it was at the correct size so execution time could take longer. I can't think of a direct way to compress the image with a php function unless you compressed the images instead of resizing them.

Laravel Image Quality

Every time I upload a new image to my server, this image is resized. The main issue I'm having is that the image is losing a lot of quality. This is the current code:
$name1 = str_random(10);
$img = Image::make($img1);
$img->resize(270,152, function ($constraint) {$constraint->aspectRatio();});
$img->crop(160, 132, 55, 0);
$img->save('/imagenes/'.$name1.'.jpg', 100);
$escena->img1_plx = $name1.'.jpg';
I tried with different changes but the image still lose many quality
You can try a new library: Intervention Image
Here is the link: Intervention Image
You can choose format, dimensions, crop and quality for the images.

Resizing images before upload when using BulletProof upload class

found this which is nice and quick to implement. It works great but what I want it before the images are uploaded, that they get resized to a max width but keeping the ratio.
Let say i am uploading an image with a width of 5000px, i want this to be resized to 1000px width but keep the height ratio and then save the final image.
Example usage:
/* shrink() - will shrink/resize the image according to the given dimensions (in pixels)
* NOTE, a folder called 'shrinked_images' will be created first to store the uploaded image
*/
$bulletProof
->fileTypes(array("jpg", "gif", "png", "jpeg"))
->uploadDir("shrinked_images")
->shrink(array("height"=>100, "width"=>200))
->upload($_FILES["pictures"]);
The GitHub:
https://github.com/samayo/bulletproof
I have read through the docs but cant find anything about resizing. All i can find in the code is the shrinking function but cant see how to add the keep ratio option with that?
Thanks. Craig.
Second parameter of shrink is $ratio which allows to preserve aspect ratio.
Try
->shrink(array("height"=>100, "width"=>200), true)
or if you want your images resized using width only set height to PHP_INT_MAX as both parameters are required
->shrink(array("height"=> PHP_INT_MAX, "width"=>200), true)

How resize image with custom ratio using Intervention image manipulation library in laravel

I want to resize an image with custom ratio (width:height)=(5:1)
Using Intervention image manipulation library in laravel.
It's not a problem if the image stretches. I don't want to put any fixed height or width.
so please give me some suggestions.
I think the best solution might be, to use fit() from the library.
Like this:
// open 4/3 image for example
$image = Image::make('foo.jpg');
// your desired ratio
$ratio = 16/9;
// resize
$image->fit($image->width(), intval($image->width() / $ratio));
It don't stretches the image.
I don't think intervention image library has this option in their resize function. you can use getimagesize() php function to get the height and width and divide width with 5 (in your case its 5 because you want 5:1) to get the height.
$image=getimagesize($image_file);
$width=$image[0]; // $image[0] is the width
$height=$image[0]/5; // $image[1] is the height
Than you can just use your intervention's resize() function to resize to that height and width.
Image::make($source_image)
->resize($width,$height ,false,false)
->save($destination);`
I choose fit() rather than resize() to modify the picture avoiding to stretch the image to much.
I use a php snippet in my project, which might be helpful.
$img = Image::make($pictureOriginalPath);
// Picture ratio
$ratio = 4/3;
// Check the current size of img is appropriate or not,
// if ratio of current img is greater than 1.33, then crop
if(intval($img->width()/$ratio > $img->height()))
{
// Fit the img to ratio of 4:3, based on the height
$img->fit(intval($img->height() * $ratio),$img->height());
}
else
{
// Fit the img to ratio of 4:3, based on the width
$img->fit($img->width(), intval($img->width()/$ratio));
}
// Save, still need throw exception
$img->save($pictureNewPath);

kohana 3.0 resize image before uploading?

I want to upload some images to the server, but first of all i want them croped, and resized to some certain dimensions.
Now i am doing the simple upload and save like that:
$header_image = Upload::save($_FILES['sale_picture_header'],NULL,APPPATH.'media'.'/');
$image_header = Model::factory('image');
$image_header->name = basename($header_image);
$image_header->save();
(excluding the validation).
How can i crop or resize the image to some desired dimensions, in Kohana 3.0? I couldn't find any relevant documentation regarding that.
Thank you!
Did you try with the image package:
// Resize to 200 pixels on the shortest side
$image->resize(200, 200);
// Resize to 200x200 pixels, keeping aspect ratio
$image->resize(200, 200, Image::INVERSE);
// Resize to 500 pixel width, keeping aspect ratio
$image->resize(500, NULL);
// Resize to 500 pixel height, keeping aspect ratio
$image->resize(NULL, 500);
// Resize to 200x500 pixels, ignoring aspect ratio
$image->resize(200, 500, Image::NONE);
source:
http://kohanaframework.org/3.0/guide/api/Image#resize
You can use the ImageMagick library, or the GD library for image manipulation.
Also note that these are strictly server-side, and will not happen before upload. For that you'd need some client-side plugin capable of image manipulation such as Flash or Java.
I found something very interesting. The question was, how to perform this action, "resize then crop" an image to fit perfectly a determined box, and this is the solution:
Image::factory($file)
->resize(128, 149, Image::PRECISE)
->crop(128, 149)
->save(DOCROOT.$filename);
Note that you must use Image::PRECISE, instead of IMAGE::AUTO.
Hope this helps someone.

Categories