kohana 3.0 resize image before uploading? - php

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.

Related

Cropping an image from the center into the largest square you can make in Yii2 Imagine?

I'm using the Yii2 extension Imagine and I need to make 150x150 images from user uploads.
Currently I am just doing something like this:
use yii\imagine\Image;
....
Image::thumbnail($save_path, $img_size, $img_size)->save($save_path);
Obviously this can cause issues if one of the dimensions is < 150px once resized.
So what I'm trying to primarily figure out is how to crop the image into a square before it is resized, so that when I resize it there won't be any aspect ratio issues.
Now, I know you can crop the image with something like:
Image::crop($save_path, $img_size, $img_size, [5, 5]);
But problem is doing this before resizing the image will likely not give you what you want since the image may be so large and cropping it after resizing won't work either as one dimension may already have been reduced to < 150px.
So what I'm trying to work out is how can I crop the image before resizing to the maximum size square possible and from the center out?
Edit:
Ok, I have worked out a way to handle this, but was wondering if there was anyway to accomplish the below easily or will I need to code it myself?
Work out the smallest dimension (width or height)
Then take that dimension and that will be the largest square you can have
Work out how to position that in the center for the crop
Now you can do the resize
If after the resize either side is smaller than 150, create new white background image and then center the new image on that
Save image
Done!
Another try :p
<?php
use yii\imagine\Image;
use Imagine\Image\Box;
use Imagine\Image\Point;
// ...
$thumbnail = Image::thumbnail($save_path, $img_size, $img_size);
$size = $thumbnail->getSize();
if ($size->getWidth() < $img_size or $size->getHeight() < $img_size) {
$white = Image::getImagine()->create(new Box($img_size, $img_size));
$thumbnail = $white->paste($thumbnail, new Point($img_size / 2 - $size->getWidth() / 2, $img_size / 2 - $size->getHeight() / 2));
}
$thumbnail->save($save_path);
Can't you just use the fourth parameter of Image::thumbnail()?
Image::thumbnail($save_path, $img_size, $img_size, Image\ImageInterface::THUMBNAIL_INSET)->save($save_path);
From http://www.yiiframework.com/doc-2.0/yii-imagine-baseimage.html#thumbnail()-detail:
If thumbnail mode is ImageInterface::THUMBNAIL_INSET, the original
image is scaled down so it is fully contained within the thumbnail
dimensions. The rest is filled with background that could be
configured via yii\imagine\Image::$thumbnailBackgroundColor and
yii\imagine\Image::$thumbnailBackgroundAlpha.

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);

Image manipulation using PHP: force 1:1 aspect ratio (sqaure images)

I am looking to add a background to images that users upload that are not square. So if they upload a tall and skinny photo I want to add a white background to the sides of the image to make the resulting image have an aspect ratio of 1:1. Is this possible using PHP or javascript?
You can use the GD library for what, with a library called Wideimage it's a breeze:
$image = WideImage::load('img_form_field_name');
$size = max($image->getHeight(), $image->getWidth());
$white = $image->allocateColor(255, 255, 255);
$image->resizeCanvas($size, $size, 'center', 'center', $white);
See the documentation and examples, many functions can even be tested interactively.
The GD library is the most commonly used image manipulation package. It's a set of functions often installed with PHP which handle image manipulation.
What you'll want to do is either scale and crop your image to a specific aspect ratio so that you place your image on a square canvas and cut off whatever does fit or
You'll want to simply resize your image to a fixed aspect ratio and place it on a square canvas with whitespace around it.
Either way, this tutorial should point you in the right direction
http://return-true.com/2009/02/making-cropping-thumbnails-square-using-php-gd/
Yep you'll want to look into either the GD library or ImageMagik. There are plenty of tutorials available for this task.
Functions like imagecreatetruecolor() etc will allow you to create a new image, and then stack the uploaded image on top of it and save it as a new file.
Yes.
http://www.php.net/manual/en/refs.utilspec.image.php

Resize image after upload

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 !

Categories