davy_yg's avatar
Level 27

avatar

Hello,

I create this avatar upload file. There is one problem I need to be able to create the avatar through faker generator avatar which I already able to create one as well as editing the avatar by uploading my own avatar.

<tr>
          <td>Group Name</td>
          <td>
            <select class="custom-select d-block w-100" name="group_id" required>
              <option value="0">Select</option>
              @foreach($glist as $item)  
                <option value="{{ $item->id }}" @if($list->group_id == $item->id) selected @endif>{{ $item->group_name }}</option>
              @endforeach  
            </select>
          </td>
      </tr>    
      <tr>
          <td>Avatar </td>
          <!-- <td><img src="{{ url(Storage::url($list->avatar)) }}" width="50%" height="50%"></td> -->
          <td><img src="{{ ($list->avatar) }}" width="25%" height="25%"></td>
      </tr>
      <tr>
          <td></td>
          <td><div style="color: #a09e9e;"><b>200px x 200px</b></div></td>
      </tr>
      <tr>
          <td>Upload</td>
          <td>
              <input type="file" name="avatar" id="avatar">
          </td>
      </tr>

This is the url through faker generator: https://www.gravatar.com/avatar/75963?d=retro&size=80

This is the url if I upload my own avatar: public/avatar/l0HuuCUKOnbf5tg0c4DJypCZicb43MaUO07DGejl.jpeg

I am confuse how should I show the uploaded image:

This only works if I uploaded my own avatar:

              <td><img src="{{ url(Storage::url($list->avatar)) }}" width="50%" height="50%"></td> 

and this only works to show my generated avatar with the faker:

          <td><img src="{{ ($list->avatar) }}" width="25%" height="25%"></td>

I would like to make both avatar works, how?

0 likes
15 replies
davy_yg's avatar
Level 27

Then, how to get the file path?

ContactFactory.php

return [
    //
    'avatar' => $faker->gravatarUrl(),
    'first_name' => $faker->text(10),
    'last_name' => $faker->text(10),
    'address' => $faker->address(10),
    'city' => $faker->city(10),
    'zip' => $faker->postcode(10),
    'country' => $faker->country(10),
    'email' => $faker->email(10),
    'phone' => $faker->numberBetween($min = 100000, $max = 999999) ,
    'note' => $faker->text(10),
    'group_id' => $faker->numberBetween($min = 1, $max = 3),
    'created_at' => $faker->dateTime()
];
Sti3bas's avatar

@davy_yg 'avatar' => $faker->image('public/avatar', 640, 480, null, false),.

Sinnbeck's avatar

@davy_yg did you even click the link? The very first line tells you

Download a remote random image to disk and return its location

1 like
davy_yg's avatar
Level 27

E:\xampp72\htdocs\niche_stack>php artisan db:seed --class=ContactTableSeeder

InvalidArgumentException : Cannot write to directory "public/avatar"

Is this public/avatar directory a symlink? or storage/app/public/avatar ?

Sinnbeck's avatar

I am pretty sure it is /public/avatar

But you can use the storage_path helper

'avatar' => $faker->image(storage_path('app/public/avatar'), 640, 480, null, false),
davy_yg's avatar
Level 27

'avatar' => $faker->image(storage_path('app/public/avatar'), 210, 210, null, false),

all the avatar are 0. and there is no avatar image in storage/app/public/avatar

Sti3bas's avatar

@davy_yg do you tried $faker->image('public/storage/avatar', 210, 210, null, false)?

Sti3bas's avatar

@sinnbeck yeah, sometimes it doesn't respond at all. That's why I've switched to using local images instead of relying on the network.

Sinnbeck's avatar

I would imagine that could be the problem. Perhaps consider using a static image on the machine instead :)

davy_yg's avatar
Level 27

I tried several times and it only works for 1 contact and also there is a long time lag on processing 3 contacts.

I don't think this is a good idea since I will have to test my program by using 10.000 data. If I have to download all the pictures I think it's gonna crash. It does not works on 3 data - except for 1 row after several times trying.

Sti3bas's avatar

@davy_yg then download n images, name them from 1 to n, place it to for example tests/Support/Files/Avatars, update your factory to something like that:

'avatar' => function() {
   $img = rand(1, 30) . '.jpg';

   $path = 'public/storage/avatars/' . $img;

   Storage::copy('tests/Support/Files/Avatars/' . $img, $path);

   return $path;
}

Please or to participate in this conversation.