Codeigniter 3 | intervention-image - codeigniter

I use library intervention-image
https://image.intervention.io/v2
I fallowing to documentation and write function:
//product default image upload
public function product_default_image_upload($path, $folder)
{
$new_name = 'img_x500_' . generate_unique_id() . '.' . pathinfo($path, PATHINFO_EXTENSION);
$new_path = 'uploads/' . $folder . '/' . $new_name;
if ($folder == 'images') {
$directory = $this->create_upload_directory('images');
$new_name = $directory . $new_name;
$new_path = 'uploads/images/' . $new_name;
}
$img = Image::make($path)->orientate();
$img = Image::canvas(32, 32, '#ff0000');
$img->resize(null, 500, function ($constraint) {
$constraint->aspectRatio();
});
$img->save(FCPATH . $new_path, $this->quality);
//add watermark
if ($this->general_settings->watermark_product_images == 1) {
$this->add_watermark(FCPATH . $new_path, 'mid');
}
return $new_name;
}
I need add background color image under product image during upload.
$img = Image::canvas(32, 32, '#ff0000');
Example I upload photo with watch on white background and I need change this white background to RED.
The problem is with this function when I upload image then this code fill full background red. But I need bg only under product.

Related

Laravel, How to automatic create thumb folder with Image/Intervation resize method

I need when upload article image, automatic upload Original Image Size and resize image in thumb folder.
but in this method i receive this error (thumb folder can not automatic create) :
Can't write image data to path (storage/upload/images/articles/1400/05/06/thumb/Bx1WRTtp9IgSZgnP8VE11627500167.jpg)
Upload Image Method :
protected function articleUploadImage($file, $folder, $size)
{
$d = jdate();
$year = $d->format('Y');
$month = $d->format('m');
$day = $d->format('d');
$direct = 'storage/upload/images/'. $folder . '/' . $year . '/' . $month . '/' . $day;
$directWithResize = 'storage/upload/images/'. $folder . '/' . $year . '/' . $month . '/' . $day . '/thumb';
$extension = $file->getClientOriginalExtension();
$fileName = Str::random(20) . time() . '.' . $extension;
// if (!is_dir($directWithResize)) {
// mkdir($directWithResize);
// }
//$path = Image::make($file->getRealPath());
//$path->resize(100, 100);
//$path->save(public_path($directWithResize . '/' . $fileName));
Image::make($file->getRealPath())->fit($size, null, function ($constraint) {
$constraint->aspectRatio();
})->save($direct . '/thumb/' . $fileName);
//$path = $file->store($direct);
//$newPath = $direct;
return $direct;
}
Before uploading an image, you should create a directory.
protected function articleUploadImage($file, $folder, $size)
{
$d = jdate();
$year = $d->format('Y');
$month = $d->format('m');
$day = $d->format('d');
$direct = 'storage/upload/images/'. $folder . '/' . $year . '/' . $month . '/' . $day;
$extension = $file->getClientOriginalExtension();
$fileName = Str::random(20) . time() . '.' . $extension;
if (!file_exists($direct . '/thumb/')) {
mkdir($direct . '/thumb/', 666, true);
}
Image::make($file->getRealPath())->fit($size, null, function ($constraint) {
$constraint->aspectRatio();
})->save($direct . '/thumb/' . $fileName);
return $direct;
}

Image pixelate on resize browser( responsive design ) after upload on laravel

After upload the image, on resize browser, or enter the project on the mobile browser, quality of image is very weak, how can i improve that ?
if($request->hasFile('image')){
$image = $request->file('image');
$filename = $request->name;
$foldername = $request->name;
$imagename = $filename .'.' . $request->image->extension();
$path = public_path('images/produse/'. $filename .'/');
if(!File::exists($path)){
File::makeDirectory($path, 0777, true, true);
}
Image::make($image)->resize(200, 200)->save( public_path('images/produse/' . $foldername . '/' . $imagename ));
}
You should try with a larger resize resolution. For example (800, 600)

Laravel deploy: storage image doesn't work correctly

After deploying my laravel project from local to Apache web server, all works correctly except images link. Here the code:
Images are stored in:
storage/app/public/photos
after i've run command:
php artisan storage:link
Images are linked at:
public/storage/photos
Controller:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photo = $file->storeAs('public/photos', 'foto-' . time() . '.' . $extension);
$user->photo = $photo;
$user->save();
}
Images are uploaded correctly on storage/app/public/photos and correctly linked in public/storage/photos but it doesn't display on frontend.
in blade, i've tried to use Storage::url to retrieve the path
{{Storage::url($user->photo)}}
and asset()
{{asset($user->photo)}}
in both cases, image doesn't exist
The public path of image is:
http://mywebsite.com/storage/photos/foto-1522914164.png
You should Use url function to show your image like below way.
url($user->photo);
I'd suggest to change the controller code as follows:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'foto-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$user->photo = 'photos/' . $photoFileName;
$user->save();
}
Then you can use {{asset($user->photo)}} in your blade.
on my webspace, it seems that the only way to display image correctly is to create a custom route that read and serve the image.
i'm solved like this:
i'm storing only image name in db:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'photo-' . $model->id . '.-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$store = $photoFileName;
}
then, i've create custom route that read images and display them:
Route::get('storage/{filename}.{ext}', function ($filename, $ext) {
$folders = glob(storage_path('app/public/*'), GLOB_ONLYDIR);
$path = '';
foreach ($folders as $folder) {
$path = $folder . '/' . $filename . '.' . $ext;
if (File::exists($path)) {
break;
}
}
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header('Content-Type', $type);
return $response;
});
in blade, i'm using Storage to display image:
{{ Storage::url($photo->photo) }}}

Image resize error using intervention in laravel 5

I am using intervention image to resize image and then save it to a destination folder. But its not working for me. I am getting error like, "Image source not readable".
Pls see the below code:
$image_name = $file->getClientOriginalName();
$thumbName = 'thumb_'. $image_name;
$destinationPath = public_path() . '/uploads/';
$thumbdestinationPath = public_path() . '/uploads/thumbnails/';
$imgUrl = URL::to('/').'/public/uploads/'.$image_name;
$thumbUrl = URL::to('/').'/public/uploads/thumbnails/'.$image_name;
$upload_success = $file->move( $destinationPath, $image_name);
if ($upload_success) {
Image::make($file->getRealPath())->fit('120','120')->save($thumbdestinationPath );
}
You need to give the name of image:
if ($upload_success) {
Image::make($file->getRealPath())->fit('120','120')->save($thumbdestinationPath . $image_name );
}

Laravel 4 upload 1 image and save as multiple (3)

I'm trying to make an image upload script with laravel 4. (using Resource Controller) and i'm using the package Intervention Image.
And what i want is: when uploading an image to save it as 3 different images (different sizes).
for example:
1-foo-original.jpg
1-foo-thumbnail.jpg
1-foo-resized.jpg
This is what i got so far.. it's not working or anything, but this was as far as i could get with it.
if(Input::hasFile('image')) {
$file = Input::file('image');
$fileName = $file->getClientOriginalName();
$fileExtension = $file->getClientOriginalExtension();
$type = ????;
$newFileName = '1' . '-' . $fileName . '-' . $type . $fileExtension;
$img = Image::make('public/assets/'.$newFileName)->resize(300, null, true);
$img->save();
}
Hopefully someone can help me out, thanks!
You may try this:
$types = array('-original.', '-thumbnail.', '-resized.');
// Width and height for thumb and resized
$sizes = array( array('60', '60'), array('200', '200') );
$targetPath = 'images/';
$file = Input::file('file')[0];
$fname = $file->getClientOriginalName();
$ext = $file->getClientOriginalExtension();
$nameWithOutExt = str_replace('.' . $ext, '', $fname);
$original = $nameWithOutExt . array_shift($types) . $ext;
$file->move($targetPath, $original); // Move the original one first
foreach ($types as $key => $type) {
// Copy and move (thumb, resized)
$newName = $nameWithOutExt . $type . $ext;
File::copy($targetPath . $original, $targetPath . $newName);
Image::make($targetPath . $newName)
->resize($sizes[$key][0], $sizes[$key][1])
->save($targetPath . $newName);
}
Try this
$file = Input::file('userfile');
$fileName = Str::random(4).'.'.$file->getClientOriginalExtension();
$destinationPath = 'your upload image folder';
// upload new image
Image::make($file->getRealPath())
// original
->save($destinationPath.'1-foo-original'.$fileName)
// thumbnail
->grab('100', '100')
->save($destinationPath.'1-foo-thumbnail'.$fileName)
// resize
->resize('280', '255', true) // set true if you want proportional image resize
->save($destinationPath.'1-foo-resize-'.$fileName)
->destroy();

Resources