简体   繁体   中英

How to convert PNG img to JPG img in Laravel

I need some help on converting PNG files to JPG. I tried this stack but with no success. Can you help me?

HTML

<div class="custom-file">
<input type="file" class="custom-file-input" id="logo" name="logo" / >
<label class="custom-file-label" for="logo"></label>
</div>

LARAVEL

$imageName = time().'.'.$request->logo->getClientOriginalExtension();
$request->logo->move(public_path('/dist/img/logo'), $imageName);
$company->logo                      = $imageName;  
$company->save(); 

At first install this Library. http://image.intervention.io/getting_started/installation

in controller import it,

use Intervention\Image\Facades\Image;

change in your code

$image = $request->file('logo');
$imageName = time().'.'."jpg";

Image::make($image)->encode('jpg', 65)
            ->save(public_path('/dist/img/logo' . $imageName));
$company->logo                      = $imageName;  
$company->save(); 

This will help you:

$clientFile = pathinfo($_FILES['logo']['name']);
if($clientFlie['extension'] == 'png'){ 
    $ImageNameWithNewExtensionForClient = $clientFile['filename']."jpg";
 }

after you can move $ImageNameWithNewExtensionForClient to any path you want.

May be your JPG Support is disabled. please check using phpinfo() .

Reference Link : https://stackoverflow.com/a/18821624/9533638

OR

you can use this one to convert png image to jpg. it will work.

=> https://github.com/free-open-source/php-image-converter/blob/master/src/ImageConverter.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM