Skip to content

Instantly share code, notes, and snippets.

@bosz
Created March 31, 2020 08:39
Show Gist options
  • Save bosz/54664c24c3756cb068b4d943d0d384a8 to your computer and use it in GitHub Desktop.
Save bosz/54664c24c3756cb068b4d943d0d384a8 to your computer and use it in GitHub Desktop.
Optimize and upload a file to s3 bucket
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
function hlp_file_upload($fileObj, $s3Path, $newDimension=null, $fname=null, $watermark = true)
{
$basePath = storage_path('app/uploads');
$ext = $fileObj->getClientOriginalExtension();
$ext = empty($ext) ? 'jpg' : $ext;
$oldFileName = $fname ? $fname : md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)) . '.' . $ext ;
$convertingFileName = $fname ? $fname : md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)) . '.jpg' ;
/*Upload the image*/
$fileObj->move($basePath, $oldFileName);
/*Convert image to jpg from what ever format it is.*/
$fileName = hlp_img_to_jpg($basePath . '/' . $oldFileName, $basePath . '/' . $convertingFileName, 50);
$filePath = $basePath . '/' . $fileName;
$imgSize = filesize($filePath)/1000000;
if ($imgSize > 1) {
$cmd = 'mogrify -quality 60 ' . $filePath;
exec($cmd);
}
/*Automatic resize image to size of width of image*/
if (is_array($newDimension) && isset($newDimension['width']) && isset($newDimension['height'])) {
list($old_width, $old_height) = getimagesize($filePath);
$new_width = $newDimension['width'];
if ($new_width < $old_width) {
$tmp_new_height = $newDimension['height'];
if ($tmp_new_height == 0) {
$new_height = (int)(($old_height / $old_width ) * $new_width);
}else{
$new_height = $tmp_new_height;
}
/*$img = Image::make($filePath)->fit($new_width, $new_height, function ($constraint) {
$constraint->upsize();
});*/
$img = Image::make($filePath)->fit($new_width, $new_height, function ($constraint) {
$constraint->aspectRatio();
});
$img->save($filePath);
}
}
/*Add watermark*/
if ($watermark) {
hlp_add_watermark($basePath, $fileName);
}
// SEND TO S3
$imageName = $s3Path . '/'.$fileName;
if(Storage::disk('s3')->put($imageName, fopen($filePath, 'r+'), 'public')){
$path = Storage::disk('s3')->url($imageName);
}else{
$path = 'https://digitalrenter.com/fr/img/no-img.jpg';
$imageName = $fileName;
}
return [
'name' => $imageName,
'path' => $path,
];
return $fileName;
}
function hlp_img_to_jpg($originalFile, $outputFile, $quality=50) {
$imgType = mime_content_type($originalFile);
if($imgType == 'image/jpeg' ){
return basename($originalFile);
}elseif($imgType == 'image/png') {
$image = imagecreatefrompng($originalFile);
}elseif($imgType == 'image/gif'){
$image = imagecreatefromgif($originalFile);
}else{
return basename($originalFile);
}
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
imagejpeg($bg, $outputFile, $quality);
imagedestroy($bg);
unlink($originalFile);
return basename($outputFile);
}
function hlp_add_watermark($basePath, $fileName)
{
$fullPath = $basePath . '/' . $fileName;
list($i_width, $i_height) = getimagesize($fullPath);
$w_min_width = $i_width/6;
$i = 0;
$w_logo = 'logo-1950.png';
while ($i < 2000) {
// $w_logo = 'logo-' . $i . '.png';
if ($i >= $w_min_width) {
$w_logo = 'logo-' . $i . '.png';
break;
}
$i = $i + 50;
}
$w_logo_path = public_path('img/logos/' . $w_logo);
// $w_logo_path = public_path('fr/img/logo-sm.png');
// Load the logo and the photo to apply the watermark to
$logo_obj = imagecreatefrompng($w_logo_path);
$im_obj = imagecreatefromstring(file_get_contents($fullPath));
// Set the margins for the logo and get the height/width of the logo image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($logo_obj);
$sy = imagesy($logo_obj);
$marge_right = ($i_width / 2) - ($sx/2);
$marge_bottom = ($i_height / 2) - ($sy/2);
imagecopy($im_obj, $logo_obj, $marge_right, $marge_bottom,
0, 0, imagesx($logo_obj), imagesy($logo_obj));
imagejpeg($im_obj, $fullPath);
imagedestroy($im_obj);
}
// Call the function like this
$image = hlp_file_upload(
$fileObj = $request->file('filename'),
$path = 'listings',
$newDimension = ['width' => 1500, 'height' => 0],
$fname = null,
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment