Laravel Image Quality - php

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.

Related

Imagick php class not working when I try to set image resolution and color space for a base64 image then saving it in jpg format

I'm trying to generate a high resolution jpg image from base24 image string using imagick php class. the code runs with no errors.
However, the image saved does not show any resolution or color space in photo viewers or browsers. I don't know whether my code is working or not?!
Any help on this is highly appreciated.
$image_data = base64_decode($data);
$imagick = new Imagick();
$imagick->readImageBlob($image_data);
$jpegimagequality = 95;
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality($jpegimagequality);
$imagick->setImageResolution(300,300);
$imagick->setImageColorspace(Imagick::COLORSPACE_SRGB);
$imageResampled = $imagick->resampleImage(300, 300, Imagick::FILTER_UNDEFINED, 1);
$imagick->setImageFormat("jpg");
$devimageSaved = $imagick->writeImage('jpg:'.$hiResPosterImagePathDev);

resize image based on query string

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

PHP - convert all images to jpg using Imagick - bad quality

I found more topics from this web site about quality with Imagick but nothing help me...
I have to save all images as JPG. I created this script:
$image_url = 'http://limuzynamercedes.pl/wp-content/uploads/2014/06/3.png';
$image_code = file_get_contents($image_url);
$img = new Imagick();
$img -> readImageBlob($image_code);
$img->setResolution(300, 300);
$d = $img->getImageGeometry();
$img->cropImage($d['width'],($d['height']-120), 0,0);
$img->setImageFormat('jpeg');
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->setCompressionQuality(100);
$img->writeImage('read.jpg');
$img->clear();
echo '<img src="read.jpg?'.time().'">';exit;
Here is original image: http://limuzynamercedes.pl/wp-content/uploads/2014/06/3.png
and here is image which was converted by my script: http://s5.ifotos.pl/img/demo1jpg_saeaxqx.jpg
Where is a problem? Why this image is always convert in bad quality?
Thanks.
The image is not in "bad quality" (there is no blurry areas found), but the difference between 2 images is caused by transparent PNG to JPG conversion.
Before you crop the image, add these two lines:
// set background to white (Imagick doesn't know how to deal with transparent background if you don't instruct it)
$img->setImageBackgroundColor(new ImagickPixel('white'));
// flattens multiple layers
$img = $img->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

Can i change image file size with php?

I made a small function to upload images then show them on my website.
My question is now, can I change the file size of a image, on same time as I upload it?
Take facebook as example. Even large images has a size on like 65kb.
If so, witch function should I use?
Thanks,
use the imagecopyresampled function:
$source_image = imagecreatefromjpeg("youtimagejpg");
$src_w= imagesx($source_image);
$src_h= imagesy($source_image);
$dest_image = imagecreatetruecolor(100, 100); //targeted width and height
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, 100, 100, $source_w, $source_h);
imagejpeg($dest_image,NULL,80);
//Replace null by the path if you want to save on the server, otherwise the image will be sent to the client intead.
If you want to reduce the size of the image without resizing it; use the imagejpeg function (with a low quality factor)
There are next ways to change image size:
Convert to another image format.
Save image with lossy compression (http://en.wikipedia.org/wiki/Lossy_compression)
Resample image with another width & height (http://php.net/manual/en/function.imagecopyresampled.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