luddinus's avatar

Imageable package

Hi.

I'm developing a package which helps with image upload, thumbnails, and saves image name in the database. (using Intervention\Image)

And when you delete a record, it will delete the files automatically as well.

e.g UserController

public function changeAvatar(ChangeAvatarRequest $request)
{
     $user = Auth::user();

     // this will upload the file, create the thumbnails and save uploaded file_path in the database
     $user->avatar = $request->file('avatar');
     $user->save();

}

User model

class User extends Model implements Imageable {
    
   // Imageable contract 
   public function imageables()
   {
        return [
            'path' => function($user) {
                return "avatars/{$user->username}";
           },
           'thumbnails' => [
               'sm' => 60,
               'lg' => 90
           ]
        ];
   }
}

ChangeAvatarRequest

class ChangeAvatarRequest extends ImageableRequest {
    protected $maxFileSize = 128; // Kb

    protected function inputName()
    {
        // with this you don't need set the rules
        // in this case rules will be ['avatar' => 'image|max:128'];
        return 'avatar';
    }
}

Any ideas to improve? I will upload to gitHub when I understand how it works.

0 likes
3 replies
IsaacBen's avatar

Maybe you should add a solution to the vertical images that are being uploaded. They always turn 90 degrees to the left. I'm using this, and so far it solves it, but it's a very annoying problem.

<?php
$hey = $images->filename; 

    $exif = @exif_read_data($hey);
    if (!empty($exif['Orientation'])) {
        $image = imagecreatefromjpeg($hey);
        switch ($exif['Orientation']) {
            case 3:
                $image = imagerotate($image, 180, 0);
                break;

            case 6:
                $image = imagerotate($image, -90, 0);
                break;

            case 8:
                $image = imagerotate($image, 90, 0);
                break;
        }

        imagejpeg($image, $hey, 90);
    }

?>
IsaacBen's avatar

Well, if I try to upload a portrait image from an iPhone it will flip 90 degrees to the side, but if it was taken on landscape mode then it's okay. Just try it yourself and you will see. If you have a mac you might not notice, but if you have a windows computer you will see what I'm talking about. You can see this complaint, but it applies to iPhone 6 as well. https://wordpress.org/support/topic/image-rotation-issue-for-iphone-5s-portrait-photos

Please or to participate in this conversation.