How to compress image before uploading in Laravel? - php

I'm making a images gallery website where users can upload any image and they will be displayed on frontend. I need to compress images without effecting it's quality to reduce there size so that page load speed should not effect that much. I'm using following code to upload image:
$rules = array('file' => 'required');
$destinationPath = 'assets/images/pages'
$validator = Validator::make(array('file' => $file), $rules);
if ($validator->passes()) {
$filename = time() . $uploadcount . '.' . $file->getClientOriginalExtension();
$file->move($destinationPath, $filename);
return $filename;
} else {
return '';
}

The best and easiest way to compress images before uploading to the server, I found here:-
https://github.com/spatie/laravel-image-optimizer

You need to optimize the image for web usage as user may upload images that are way to large (Either in size or resolution). You may also want to remove the meta data from the images to decrease the size even more. Intervention Image perfect for resizing/optimizing images for web usage in Laravel. You need to optimize the image before it is saved so that the optimized version is used when loading the web page.
Intervention Image

https://tinypng.com provides an API service for compressing images. All you need to do is install their PHP library in Laravel, get a developer key from their website. After that by the adding the below code, you can compress your uploaded image. In the code, I am assuming you have stored your file under 'storage' directory.
$filepath = public_path('storage/profile_images/'.$filename);
\Tinify\setKey("YOUR_API_KEY");
$source = \Tinify\fromFile($filepath);
$source->toFile($filepath);
Here is the link to a blog which explains how to upload and compress images in Laravel http://artisansweb.net/guide-upload-compress-images-laravel

**Using core php **
function compress($source_image, $compress_image)
{
$image_info = getimagesize($source_image);
if ($image_info['mime'] == 'image/jpeg') {
$source_image = imagecreatefromjpeg($source_image);
imagejpeg($source_image, $compress_image, 20); //for jpeg or gif, it should be 0-100
} elseif ($image_info['mime'] == 'image/png') {
$source_image = imagecreatefrompng($source_image);
imagepng($source_image, $compress_image, 3);
}
return $compress_image;
}
public function store(Request $request)
{
$image_name = $_FILES['image']['name'];
$tmp_name = $_FILES['image']['tmp_name'];
$directory_name = public_path('/upload/image/');
$file_name = $directory_name . $image_name;
move_uploaded_file($tmp_name, $file_name);
$compress_file = "compress_" . $image_name;
$compressed_img = $directory_name . $compress_file;
$compress_image = $this->compress($file_name, $compressed_img);
unlink($file_name);
}

Related

Laravel : Upload 64base image instead of normal

I have an application using React and Laravel where I'm uploading images.
I'm using Laravel Image Intervention package.
When testing my API, the upload for a normal image works perfectly.
Using React, the user can select an image from his pc and crop it, then he sees the preview as a generated cropped 64base image.
I would want to upload the generated 64base image to my database instead of a normal one, how to do so ?
Fileupload controller
public function Store(Request $request)
{
$this->validate($request, [
'filename' => 'image|required|mimes:jpeg,png,jpg,gif,svg'
]);
$originalImage= $request->file('filename');
$thumbnailImage = Image::make($originalImage);
// Define upload path
$originalPath = public_path('/images/');
// Save Orginal Image
$thumbnailImage->save($originalPath.time().$originalImage->getClientOriginalName());
// Save In Database
$imagemodel= new Photo();
$imagemodel->photo_name=time().$originalImage->getClientOriginalName();
$imagemodel->save();
return back()->with('success', 'Image Upload successful');
}
This one should work
$folderPath = "images/";
$image_parts = explode(";base64,", $request->file('file'));
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$file = $folderPath . uniqid() . '. '.$image_type;
// file_put_contents($file, $image_base64);
Storage::put($file, $image_base64);
Possible alternative:
$image = Image::make(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $request->file('file'))))->stream();
Storage::put($file, $image, 'public');

Why $conigf['quality'] parameter not working with png file but works awesome with jpg while image upload in codeigniter?

I have been trying to use imagemagic in my codeigniter API. The problem is that below code works fine with my jpg image compression. When I upload ".jpg" image of 2MB size it successfully resizes it to perfect 80kb file on server but when I use ".png" extension the size goes down but not that much it still gives me image size of 300kb and I observed that with png format "quality" does not effect. I tried putting quality 100 and 50 but still same result of big image size for ".png".
And also that if I remove "width" and "height" config parameter than only "quality" does not affect the image file it uploads image of very big size... no image file size compression bases on "quality" parameter.
Can anyone help where I am making mistake or missing something???
My API:
public function multi_image_upload()
{
$aa = array();
if (!empty($_FILES['images'])) {
foreach ($_FILES['images']['name'] as $key => $image) {
$extFile = $_FILES['images']['name'][$key];
$ext = pathinfo($extFile, PATHINFO_EXTENSION);
$nm = date('ymdhis') . '-bingo' . rand(11111, 99999);
$filnm = $nm . ".png";
$file = APPPATH . '../upld/biz/' . $filnm;
$newname = $filnm;
$source_image = $file;
if (move_uploaded_file($_FILES['images']['tmp_name'][$key], $file)) {
$this->fun->imageupload("upld/biz/", $newname, $source_image);
if (file_exists($file)) {
unlink($file);
}
}
}
$res = $this->responce(false, $aa);
} else {
$res = $this->responce(true, "Please select image");
}
echo $res;
}
IMAGE UPLOAD MODEL:
public function imageupload($url, $newname, $source_image)
{
$path = APPPATH . '../' . $url;
$imgsz = array(256,700);
foreach ($imgsz as $imxs) {
$sizefolder = $imxs . "x" . $imxs . "/";
if (!file_exists($path . $sizefolder)) {
mkdir($path . $sizefolder, 0777);
}
$size = getimagesize($source_image);
$filekb = filesize($source_image); // $config['image_library'] = 'gd2';
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin/';
$config['overwrite'] = TRUE;
$config['source_image'] = $source_image;
$config['new_image'] = $path . $sizefolder . $newname;
$config['quality'] = 100;
if ($size[1] > $size[0]) {
$config['height'] = $imxs;
} else {
$config['width'] = $imxs;
}
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
JPEG is a lossy compression, meaning that you can limit the size of the output image by setting quality settings, resulting in lower quality image and also lower file size.
PNG is a lossless compression, so you cannot limit quality - it always stays the same. But you can limit compression - the effort that Imagemagick puts into making the PNG file smaller - but like with e. g. ZIP compression the output size is largely dependent on data type - and most images do not compress particularly well.
Also see Compress a PNG image with ImageMagick for some more options on limiting PNG file sizes.

Encoding format (tmp) is not supported for image resize laravel

i want to resize uploaded image and store in folder.then show in web.
i used enctype="multipart/form-data" on form in blade.php.
file successfully show in web without resize.
when try to resize image i got error
controller.php
public function dili(Request $request)
{
$di = new diligent;
$di->jobtype = $request->jobtype;
$di->jobC = $request->jobC;
$di->details = $request->details;
$image = $request->file('image');
$path = $image->getClientOriginalName();
$destinationPath = public_path('img');
Image::make($image)->resize(300, 100)->save($image);
$a = $image->move($destinationPath, $path);
$di->image = $path;
$di->save();
$de = diligent::all();
return view('admin')->with('dw', $de);
}
Error Message
Encoding format (tmp) is not supported.
1) use getRealPath() inside Image::make()
2) save image in particular path. try like this.
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 100);
$image_resize->save(public_path('img/' .$filename));
}
Make sure you installed Image intervention library.
The Intervention image save() method requires a filename so it knows what file format (jpg, png, etc..) to save your image in.
The reason you are getting the error is it does not know what encoding to save the temporary image object (tmp) in.
Here is an example
->save('my-image.jpg', 90)
There is also a optional second parameter that controls the quality output. The above outputs at 90% quality.
http://image.intervention.io/api/save

Generate a thumbnail of any upload file

I have a website where users upload document files. I want to display thumbnails of these documents. How can I display the first page of a document file as an image using codeigniter?
Below is the controller code:
public function do_upload()
{
require_once APPPATH . "php_include/authenticate.php";
$path = "uploads/file_attachment/";
if($_FILES['userfile']['name'] != ''){
$image = $_FILES['userfile']['name'];
$ext = pathinfo($image, PATHINFO_EXTENSION);
$filename = basename($image,'.'.$ext);
$filename = preg_replace("/[^a-zA-Z0-9]+/", "-", $filename);
$image = $filename.'.'.$ext;
move_uploaded_file($_FILES["userfile"]["tmp_name"],$path.$image);
$data = array(
'user_id' => $user_id,
'file' => $image,
);
$this->signupmodel->addfile($data);
}
}
This may be a bit of overkill for you but I described a bulletproof way to upload images. There are all kinds of ways where the extension (.jpg, .gif) does not match the image. Browsers have to do similar testing before rendering an image.
This code also chooses the most efficient image type to save it as and scales the image to multiple dimensions.
Stackoverflow: Scaling Images

resize width and compress images on upload php mysql

I have a client that sends me text messages from his iPhone with images for me to upload into his gallery. I'm trying to create a admin system so I can simply take the images from the texts, go to the admin page on my iPhone and upload the images straight to the gallery.
This would save me tons of time in my day to day work schedule.
Using the provided code. How can I add the following functions:
I would like to compress the file size down to a smaller size if possible, similar to the save to web jpg function in Photoshop. (Most images I get are around 1-3 MB. I would like to get them down to around 150-500kb max)
I would like to automatically change the width to 760px, but keep the aspect ratio so the images are not squished. He sends me landscape and portrait images.
Beings they are iPhone images. They have an extension .JPG (all caps) I would like this to change to .jpg (all lower case.) This is not a deal breaker I would just like to know how to do this for future use.
Either one of these functions would be very helpful, but all 3 would be ideal for my situation.
Here is the code I'm working with?
THIS IS THE FINAL CORRECT CODE FOR UPLOADING AND RESIZING IMAGES PROVIDED By #tman
Make sure you have imagick installed in your php.ini file. Check with your hosting provider to install it.
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
for($i=0;$i<count($_FILES["image"]["name"]);$i++){
if($_FILES["image"]["name"][$i] != ''){ // don't insert if file name empty
$dataType = mysql_real_escape_string($_POST["dataType"][$i]);
$title = mysql_real_escape_string($_POST["title"][$i]);
$fileData = pathinfo($_FILES["image"]["name"][$i]);
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName;
if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path)){ // The file is in the images/gallery folder.
// Insert record into database by executing the following query:
$sql="INSERT INTO images (data_type, title, file_name) "."VALUES('$dataType','$title','$fileName')";
$retval = mysql_query($sql);
///NEW
$size = getimagesize($target_path);
$width=$size[0];
$height=$size[1];
$newwidth = 760;
$newheight = $height*($newwidth/$width);
$pic = new Imagick($target_path);//specify name
$pic->resizeImage($newwidth,$newhight,Imagick::FILTER_LANCZOS,1);
unlink($target_path);
$pic->writeImage($target_path);
$pic->destroy();
///NEW
echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />
<a href='index.php'>Add another image</a><br />";
}
else
{
echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
}
}
} // close your foreach
?>
uploader.php Original code. Allows me to upload 4 images at once. WORKS!!!
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
for($i=0;$i<count($_FILES["image"]["name"]);$i++){
if($_FILES["image"]["name"][$i] != ''){ // don't insert if file name empty
$dataType = mysql_real_escape_string($_POST["dataType"][$i]);
$title = mysql_real_escape_string($_POST["title"][$i]);
$fileData = pathinfo($_FILES["image"]["name"][$i]);
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName;
if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path)){ // The file is in the images/gallery folder.
// Insert record into database by executing the following query:
$sql="INSERT INTO images (data_type, title, file_name) "."VALUES('$dataType','$title','$fileName')";
$retval = mysql_query($sql);
echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />
<a href='index.php'>Add another image</a><br />";
}
else
{
echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
}
}
} // close your foreach
?>
FYI, This will allow you to give a unique names to your images, resize the width, but keep the correct aspect ratio and upload multiple file at the same time.
Awesome Stuff!
Like this:
$filelocation='http://help.com/images/help.jpg';
$newfilelocation='http://help.com/images/help1.jpg';
$size = getimagesize($filelocation);
$width=$size[0];//might need to be ['1'] im tired .. :)
$height=$size[1];
// Plz note im not sure of units pixles? & i could have the width and height confused
//just had some knee surgery so im kinda loopy :)
$newwidth = 760;
$newheight = $height*($newwidth/$width)
$pic = new Imagick( $filelocation);//specify name
$pic->resizeImage($newwidth,$newhight,Imagick::FILTER_LANCZOS,1);
//again might have width and heing confused
$pic->writeImage($newfilelocation);//output name
$pic->destroy();
unlink($filelocation);//deletes image
Here is something kind of similar, lets check the size and compress if the image seems that it is too big. I didn't resize it which just requires that you get the dimensions and resize based on desire.
All this is doing is if the file is greater than 250KB compress it to 85% ..
$bytes = filesize($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
//$maxSizeInBytes = 26400; //is 250KB? No? compress it.
if ($bytes > 26400) {
$img = new Imagick($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->stripImage();
$img->setImageCompressionQuality(85);
$img->writeImage($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
}
OR:
// resize with imagejpeg ($image, $destination, $quality); if greater than byte size KB
// Assume only supported file formats on website are jpg,jpeg,png, and gif. (any others will not be compressed)
$bytes = filesize($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
//$maxSizeInBytes = 26400; //is gtr than 250KB? No? compress it.
if ($bytes > 26400) {
$info = getimagesize($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
$quality = 85; //(1-100), 85-92 produces 75% quality
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
imagejpeg($image,$inventory_path.DIRECTORY_SEPARATOR.$this->uploadName,$quality);
} elseif ($info['mime'] == 'image/gif') {
$image = imagecreatefromgif($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName);
imagejpeg($image,$inventory_path.DIRECTORY_SEPARATOR.$this->uploadName,$quality);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($inventory_path.DIRECTORY_SEPARATOR.$this->uploadName
imagejpeg($image,$inventory_path.DIRECTORY_SEPARATOR.$this->uploadName,$quality);
}
}

Categories