#956 create optimized images for ads #172

This commit is contained in:
Diatrex 2020-05-01 17:16:10 +03:00
parent 8ede266bc2
commit c6e5c6635b
7 changed files with 142 additions and 32 deletions

View File

@ -25,6 +25,10 @@ return [
'default_published_time', 'default_published_time',
'default_adv_limit', 'default_adv_limit',
'default_GET', 'default_GET',
'thumbnail_width',
'thumbnail_height',
'picture_width',
'picture_height',
'watermark_type', 'watermark_type',
'watermark_text', 'watermark_text',
'watermark_image', 'watermark_image',

View File

@ -111,6 +111,30 @@ return [
'default_value' => 0, 'default_value' => 0,
], ],
], ],
'thumbnail_width' => [
'type' => 'anomaly.field_type.integer',
'config' => [
'default_value' => 270,
],
],
'thumbnail_height' => [
'type' => 'anomaly.field_type.integer',
'config' => [
'default_value' => 180,
],
],
'picture_width' => [
'type' => 'anomaly.field_type.integer',
'config' => [
'default_value' => 900,
],
],
'picture_height' => [
'type' => 'anomaly.field_type.integer',
'config' => [
'default_value' => 600,
],
],
'watermark_type' => [ 'watermark_type' => [
'type' => 'anomaly.field_type.select', 'type' => 'anomaly.field_type.select',
'bind' => 'adv.watermark_type', 'bind' => 'adv.watermark_type',

View File

@ -82,6 +82,18 @@ return [
'default_GET' => [ 'default_GET' => [
'name' => 'Default Ad GET', 'name' => 'Default Ad GET',
], ],
'thumbnail_width' => [
'name' => 'Thumbnail Width',
],
'thumbnail_height' => [
'name' => 'Thumbnail Height',
],
'picture_width' => [
'name' => 'Picture Width',
],
'picture_height' => [
'name' => 'Picture Height',
],
'twitter' => [ 'twitter' => [
'name' => 'Twitter', 'name' => 'Twitter',
], ],

View File

@ -11,7 +11,7 @@
<a href="{{ image }}"><img src="{{ img('visiosoft.theme.base::images/no-image.png').url }}"></a> <a href="{{ image }}"><img src="{{ img('visiosoft.theme.base::images/no-image.png').url }}"></a>
{% else %} {% else %}
{% for image in adv.getViewPhotoUrl %} {% for image in adv.getViewPhotoUrl %}
<a href="{{ image }}"><img src="{{ image }}"></a> <a href="{{ adv.getMediumPhotoUrl(image) }}"></a>
{% if loop.index == 1 %} {% if loop.index == 1 %}
{% set advPhoto = image %} {% set advPhoto = image %}
{% endif %} {% endif %}

View File

@ -19,6 +19,11 @@ class AdvPresenter extends EntryPresenter
} }
public function getMediumPhotoUrl($fullPhotoUrl)
{
$mediumPhotoUrl = pathinfo($fullPhotoUrl);
return $mediumPhotoUrl['dirname'] . '/md-' . $mediumPhotoUrl['basename'];
}
public function isAdVideo() public function isAdVideo()
{ {

View File

@ -1,14 +1,16 @@
<?php namespace Visiosoft\AdvsModule\Adv; <?php namespace Visiosoft\AdvsModule\Adv;
use Anomaly\FilesModule\File\Contract\FileRepositoryInterface;
use Anomaly\FilesModule\Folder\Contract\FolderRepositoryInterface;
use Anomaly\SettingsModule\Setting\Contract\SettingRepositoryInterface; use Anomaly\SettingsModule\Setting\Contract\SettingRepositoryInterface;
use Anomaly\Streams\Platform\Model\Advs\AdvsAdvsEntryModel; use Anomaly\Streams\Platform\Model\Advs\AdvsAdvsEntryModel;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image;
use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface; use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface;
use Anomaly\Streams\Platform\Entry\EntryRepository; use Anomaly\Streams\Platform\Entry\EntryRepository;
use Visiosoft\CatsModule\Category\CategoryModel; use Visiosoft\CatsModule\Category\CategoryModel;
use Visiosoft\AdvsModule\Category\Contract\CategoryRepositoryInterface; use Visiosoft\AdvsModule\Category\Contract\CategoryRepositoryInterface;
use Visiosoft\DopingsModule\Doping\DopingModel;
use Visiosoft\LocationModule\City\CityModel; use Visiosoft\LocationModule\City\CityModel;
use Visiosoft\LocationModule\Country\CountryModel; use Visiosoft\LocationModule\Country\CountryModel;
@ -22,6 +24,16 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface
*/ */
protected $model; protected $model;
/**
* @var FileRepositoryInterface
*/
private $fileRepository;
/**
* @var FolderRepositoryInterface
*/
private $folderRepository;
/** /**
* Create a new AdvRepository instance. * Create a new AdvRepository instance.
* *
@ -29,11 +41,15 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface
*/ */
public function __construct( public function __construct(
AdvModel $model, AdvModel $model,
SettingRepositoryInterface $settings SettingRepositoryInterface $settings,
FileRepositoryInterface $fileRepository,
FolderRepositoryInterface $folderRepository
) )
{ {
$this->model = $model; $this->model = $model;
$this->settings = $settings; $this->settings = $settings;
$this->fileRepository = $fileRepository;
$this->folderRepository = $folderRepository;
} }
/** /**
@ -291,8 +307,32 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface
public function cover_image_update($adv) public function cover_image_update($adv)
{ {
if (count($adv->files) != 0) { if (count($adv->files) != 0) {
$file_url = 'files/images/' . $adv->files[0]->name; $fileName = 'tn-' . $adv->files[0]->name;
$adv->update(['cover_photo' => $file_url]); $folder = $this->folderRepository->findBySlug('images');
$thumbnail = $this->fileRepository->findByNameAndFolder($fileName, $folder);
if (!$thumbnail) {
// Create thumbnail image
$image = Image::make($adv->files[0]->url());
$image->resize(
setting_value('visiosoft.module.advs::thumbnail_width'),
setting_value('visiosoft.module.advs::thumbnail_height')
);
$fileName = 'tn-' . $adv->files[0]->name;
$image->save(app_storage_path() . '/files-module/local/images/' . $fileName);
// Create file entry for the image
$this->fileRepository->create([
'folder_id' => $folder->getId(),
'name' => $fileName,
'disk_id' => 1,
'size' => $image->filesize(),
'mime_type' => $image->mime,
'extension' => $image->extension,
]);
$file_url = 'files/images/' . $fileName;
$adv->update(['cover_photo' => $file_url]);
}
} }
} }

View File

@ -59,38 +59,63 @@ class UploadController extends AdminController
$watermarktype = $settings->value('visiosoft.module.advs::watermark_type'); $watermarktype = $settings->value('visiosoft.module.advs::watermark_type');
$position = $settings->value('visiosoft.module.advs::watermark_position'); $position = $settings->value('visiosoft.module.advs::watermark_position');
$img = WaterMark::make($this->request->file('upload')->getRealPath()) $fullImg = WaterMark::make($this->request->file('upload')->getRealPath())
->resize(setting_value('visiosoft.field_type.media::imageResizeW', null), setting_value('visiosoft.field_type.media::imageResizeH', 600)) ->resize(
->resizeCanvas(setting_value('visiosoft.field_type.media::imageCanvasW', 800), setting_value('visiosoft.field_type.media::imageCanvasH', 600), 'center', false, 'fff'); setting_value('visiosoft.field_type.media::imageResizeW', null),
if ($watermarktype == 'image') { setting_value('visiosoft.field_type.media::imageResizeH', 600)
)
$watermarkimage_id = $settings->value('visiosoft.module.advs::watermark_image'); ->resizeCanvas(
$watermarkimage = $files->find($watermarkimage_id); setting_value('visiosoft.field_type.media::imageCanvasW', 800),
$w = $img->width(); setting_value('visiosoft.field_type.media::imageCanvasH', 600),
if ($watermarkimage != null) { 'center',
$watermark = WaterMark::make(app_storage_path() . '/files-module/local/' . $watermarkimage->path()); false,
$img->insert($watermark, $position); 'fff'
);
$mdImg = WaterMark::make($this->request->file('upload')->getRealPath())
->resize(
setting_value('visiosoft.module.advs::picture_width'),
setting_value('visiosoft.module.advs::picture_height')
);
foreach ([$fullImg, $mdImg] as $index => $image) {
if ($watermarktype == 'image') {
$watermarkimage_id = $settings->value('visiosoft.module.advs::watermark_image');
$watermarkimage = $files->find($watermarkimage_id);
if ($watermarkimage != null) {
$watermark = WaterMark::make(app_storage_path() . '/files-module/local/' . $watermarkimage->path());
$image->insert($watermark, $position);
}
} else {
$watermarktext = $settings->value('visiosoft.module.advs::watermark_text');
$v = "top";
$h = "center";
$w = $image->width() / 2;
$h1 = $image->height() / 2;
$font_size = $w / 20;
$image->text($watermarktext, $w, $h1, function ($font) use ($v, $h, $font_size) {
$font->file(public_path('Antonio-Bold.ttf'));
$font->size($font_size);
$font->align($h);
$font->valign($v);
});
} }
if ($index === 0) {
$fileName = $file->getAttributes()['name'];
} else {
$fileName = 'md-' . $file->getAttributes()['name'];
} else { $files->create([
$watermarktext = $settings->value('visiosoft.module.advs::watermark_text'); 'folder_id' => $this->request->get('folder'),
$v = "top"; 'name' => $fileName,
$h = "center"; 'disk_id' => 1,
$w = $img->width() / 2; 'size' => $image->filesize(),
$h1 = $img->height() / 2; 'mime_type' => $image->mime,
$font_size = $w / 20; 'extension' => $image->extension,
$img->text($watermarktext, $w, $h1, function ($font) use ($v, $h, $font_size) { ]);
$font->file(public_path('Antonio-Bold.ttf')); }
$font->size($font_size); $image->save(app_storage_path() . '/files-module/local/images/' . $fileName);
$font->align($h);
$font->valign($v);
});
} }
$img->save(app_storage_path() . '/files-module/local/images/' . $file->getAttributes()['name']);
return $this->response->json($file->getAttributes()); return $this->response->json($file->getAttributes());
} }
return $this->response->json(['error' => 'There was a problem uploading the file.'], 500); return $this->response->json(['error' => 'There was a problem uploading the file.'], 500);