I'm using Laravel. I want to resize images and create thumbnail based on query string.
For example, if someone requests example.com/1.jpg?width=120 or example.com/anything/1.jpg?width=120, the original image must change to the new resized image.
In fact, all I want is the routing system for image files like .jpg and .png where has a query string.
is there any way in PHP or Laravel to get all request for images files with query string and manipulate it?
Update:
I tested #iftikhar-uddin answer.
it work for single request. like when i request this url directly example.com/anything/1.jpg?width=120 in browser.
but i want to get all images and manipulate them when page is loading.
example :
i have multiple html tag like this <img src="/anything/1.jpg?width=120">
and when page is loading, i want to get all images and manipulate them by the size of query string.
what i did before ?
currently i wrote a class for this. but the problem is i can't find original directory of images in some case.
in my class :
1- i get image source and size in image tag like this <img src="{{class::cache($model->image, 'small')}}">
2- then i resize image based on size in my class (with image.intervention.io).
3- but in some case (like when i'm using lfm package) the route of image and the real directory are different. so i get error when i want resize image based on source.(the directory is '/public/share/image.jpg' but route is 'laravel-filemanager/share/image.jpg')
for that reason, i'm searching for a way to get images by url when page is loading, not by source we insert in image tag. i think this way must be much easier.
Tips 1:
http://image.intervention.io/
Intervention Image is an open source PHP image handling and
manipulation library. It provides an easier and expressive way to
create, edit, and compose images and supports currently the two most
common image processing libraries GD Library and Imagick.
public Intervention\Image\Image resize (integer $width, integer $height, [Closure $callback])
Resizes current image based on given width and/or height.
To constrain the resize command, pass an optional Closure callback as the third parameter.
// create instance
$img = Image::make('public/foo.jpg')
// resize image to fixed size
$img->resize(300, 200);
// resize only the width of the image
$img->resize(300, null);
// resize only the height of the image
$img->resize(null, 200);
// resize the image to a width of 300 and constrain aspect ratio (auto height)
$img->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
});
// resize the image to a height of 200 and constrain aspect ratio (auto width)
$img->resize(null, 200, function ($constraint) {
$constraint->aspectRatio();
});
// prevent possible upsizing
$img->resize(null, 400, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
Tips2:
http://php.net/manual/en/function.imagecopyresized.php Using PHP functions
Install Image intervention first
Then in your controller do something like this:
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$width = $request->input('width');
$height = $request->input('height');
$image_resize = Image::make($image->getRealPath());
$image_resize->resize($width, $height );
$image_resize->save(public_path('YourImagesPath' .$filename));
}
Related
When i write a code to display image in blade file that time a image thumbnails made and i can give the every time height,width that time i need image that kind of image thumbnails generates so i can generate multiple thumb of same image in single site.
{{$page->image,100,100)}}
Or
<td><?php if ($page->image) { ?><img src="{{ url('/upload/pages/'.$page->image,100,200) }}"/><?php } ?></td>
Please Give the suggestion how to make this kind of Thumbnails Thanks In advance.
A rational way to do this would be:
Create a controller and a method (or a method on existing controller) that accepts image name, width & height parameters. This method could use Intervention package that was mentioned in the comments. Logic should be - first check if image of specified dimensions exists, if it doesn't - create it (using Intervention, very simple). Then - output the image contents (don't forget to add correct header).
Add this controller/method to routes.php, eg:
Route::get('thumbnails/{image}', 'Controller#getThumbnail');
In your Blade templates you would simply refer to images like '/path/image.jpg?width=200&height=100'. No need to care whether the file with these dimensions already exists or not.
PS. Facebook serves images this way. It's basically like a little proxy server (your method works as a proxy) between user and original image.
you need to require Intervention, then put something like this in your upload controller :
...
$file = $request->file('files');
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put('/'.$file->getFilename().$extension, File::get($file));
$thumb1 = ImageManagerStatic::make($file->getRealPath())->resize(200, 200, function ($constraint) {
$constraint->aspectRatio(); //maintain image ratio
})->save('thumb1'.$extension);
$thumb2 = ImageManagerStatic::make($file->getRealPath())->resize(400, 400, function ($constraint) {
$constraint->aspectRatio(); //maintain image ratio
})->save('thumb2'.$extension);
...
You may resize that image on the fly with a microservice and cache the thumbnails in a CDN. Please have a look here
<img src="{{ imgProxy('https://your-microservice.com/your-image.jpg', 100, 200) }}"/>
Just for the record, if you are using Laravel, a good option is to use intervention/image and intervention/imagecache packages.
What you need is described on the "URL based image manipulation" section of Intervention Image Docs.
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.
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);
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 !
I have an image which i am going to be using as a background image and will be pulling some other images from the database that i want to show within this image. So if i am pulling only 1 image, i want the bottom part of the background image to close after the first image, if there are multiple images then i want it close after those images are shown. The problem with not using separate images is that the borders of the images have a design format and i cannot show them separately.
Take a look at this image . The design format of the right and left borders are more complicated than that to just crop them and use them. Any suggestions if there is any dynamic image resizing thing?
Yes there is. Look at the imageXXXX functions; the ones you are particularly interested in are imagecreate, imagecreatetruecolor, imagecreatefrompng, imagecopyresampled, imagecopyresized, and imagepng (assuming you're dealing with PNG images - there's similar load / save functions for jpeg, gif, and a few other formats).
You should try using the GD extension for PHP, especially have a look at imagecopyresized(). This allows you to do some basic image conversion and manipulation very easily.
A basic example that takes two GET parameters, resizes our myImage.jpg image and outputs it as a PNG image:
<?php
// width and height
$w = $_GET['w'];
$h = $_GET['h'];
// load image
$image = imagecreatefromjpeg('myImage.jpg');
// create a new image resource for storing the resized image
$resized = imagecreatetruecolor($w, $h);
// copy the image
imagecopyresized($resized, $image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));
// output the image as PNG
header('Content-type: image/png');
imagepng($resized);
Have you tried PHPThumb? I used this class often and its pretty clean and lightweight. I used it here.