PHP uploading images in the correct dimensions - php

I know this question is very common but the main point in my question is that i want to know how facebook or some other websites upload their pictures in the correct size. for example. if we upload a picture of the dimension : width : 1000px , height : 20px. then facebook updates the status updates with that pic but makes the image size small in the correct ratio. its jus scales down the picture but we can know that the image is very widthy (my own word for very high width :P) where as long heighty pictures are also posted in the correct proportion.
I have included the image examples above. how does facebook calculate the size n ratio of the pics and echo out the pic by keeping the right dimensions but scaling it down at the same time.

This code is fairly verbose, but might give you an idea on how to calculate image dimensions.
Parameters are your source width and your target maximum resize width and heights
function image_resize_dimensions($source_width,$source_height,$thumb_width,$thumb_height)
{
$source_ratio = $source_width / $source_height;
$thumb_ratio = $thumb_width / $thumb_height;
// Ratio is Taller
if ($thumb_ratio > $source_ratio)
{
$result_height = $thumb_height;
$result_width = $thumb_height * $source_ratio;
}
// Ratio is Wider
elseif ($thumb_ratio < $source_ratio)
{
$result_width = $thumb_width;
$result_height = $thumb_width / $source_ratio;
}
// Ratio the Same
elseif($thumb_ratio == $source_ratio)
{
$result_height = $thumb_height;
$result_width = $thumb_width;
}
return array('x'=>$result_width,'y'=>$result_height);
}

I think there are a few factors.
the main factor is the width of the content the image is displayed in
the original width of the image
whether you want to fill the whole content with the image or not (Facebook doesn't)
Let's say you have an image which has a width of 1000px. Your content is 400px. You want to fill the content a half with your image, so you have an end width of 200px for your image. The height is just ca percental calculation (rule of three).
In the end, you will have the correct ratio and it fits well in your content.
Of course you could also check whether the image is 16:9 or 4:3 to determine the max width for images like your first example.

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)

Resize uploaded image width and scale height accordingly and opposite

I'm working on upload script and I would like to resize uploaded image so that maximum width and height are 500 pixels.
For example, if width is greater than height, then resize width to 500 pixels, and scale height accordingly, so that original aspect ratio is preserved.
And opposite, if height is greater than width, then resize height to 500 pixels and scale width accordingly.
Can someone help me out with this?
You could use PHP's ImageMagick class to do this.
You could find ImageMagick's full documentation here.
Here is a code that will resize your image to a width of 500.
$image = new Imagick();
$image->readimage($filename);
$width=$image->getimagewidth();
if ($width!='500') {
$image->resizeImage('500', '0' ,Imagick::FILTER_LANCZOS,1);
}
$image->writeImage($filename);
The zero in resizeImage('500', '0' ,Imagick::FILTER_LANCZOS,1); indicates that you will scale your height accordingly. You should look at ImageMagick's documentation, it provides cool stuff to play with images.

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

Resize image from another server and show it in my website

I wanted to show image from another server, but a smaller image maintaining the aspect ratio and i will not be knowing the exact size of that image. Can i do this?
First you need to grab the image from another server, for that you can use file_get_contents().
Then you need some code to resize an image. Since you have the image content in a string rather than a local file, you can use imagecreatefromstring() try here:
curl and resize remote image
If you want to resize it down relative to the original then use getimagesizefromstring() to get the current size, then simply calculate the new size based on your chosen percentage.
$content = file_get_contents('http://site.com/image.jpg');
$originalSize = getimagesizefromstring($content);
$originalWidth = $originalSize[0];
$originalheight = $originalSize[1];
$newSize = 0.6; // 60% of the original size
$newWidth = $originalWidth * $newSize;
$newHeight = $originalHeight * $newSize;
// now you know what the resized width and height should be, you can go ahead and do the actual resizing

Categories