-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from XIMDEX/develop
Develop
- Loading branch information
Showing
12 changed files
with
693 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,124 @@ | ||
<?php | ||
|
||
namespace App\Services\Media; | ||
|
||
use Intervention\Image\Image; | ||
use Intervention\Image\ImageManager; | ||
|
||
// JAP ELIMINAR REDUNDANCIA DE ARRAY DE TAMAÑOS con archivo ResourceController. Eliminar entrada para default ya que se trata como large o avif | ||
class MediaSizeImage | ||
{ | ||
private array $allowed_sizes = ['thumbnail', 'small', 'medium', 'raw', 'default']; | ||
private array $sizes = [ | ||
'thumbnail' => array('width' => 256, 'height' => 144), | ||
'small' => array('width' => 426, 'height' => 240), | ||
'medium' => array('width' => 854, 'height' => 480), | ||
'raw' => 'raw', | ||
'default' => array('width' => 1280, 'height' => 720) | ||
]; | ||
private array $qualities = [ | ||
'thumbnail' => 25, | ||
'small' => 25, | ||
'medium' => 50, | ||
'raw' => 'raw', | ||
'default' => 90 | ||
]; | ||
private array $sizes; | ||
private string $size; | ||
private ImageManager $manager; | ||
private Image $image; | ||
private string $path; | ||
private float $height; | ||
private float $width; | ||
public function __construct(string $size, string $path,ImageManager $manager,Image $image) | ||
{ | ||
$this->size = $size; | ||
$this->manager = $manager; | ||
$this->image = $image; | ||
$this->path = $path; | ||
$this->height = $this->image->height(); | ||
$this->width = $this->image->width(); | ||
} | ||
/** | ||
* Save the image. | ||
* | ||
* @return void | ||
*/ | ||
public function save(){ | ||
$pathSave = $this->image->dirname."/__".$this->size.".jpg"; | ||
$aspectRatio = $this->getAspectRatio($this->width / $this->height); | ||
if ($this->size === 'default'){ | ||
public function __construct(string $size, string $path, ImageManager $manager, Image $image,array $sizes) | ||
{ | ||
$this->sizes = $sizes; | ||
$this->size = $size; | ||
$this->manager = $manager; | ||
$this->image = $image; | ||
$this->path = $path; | ||
$this->height = $this->image->height(); | ||
$this->width = $this->image->width(); | ||
} | ||
/** | ||
* Save the image. | ||
* | ||
* @return void | ||
*/ | ||
public function save(String $extension) | ||
{ | ||
$pathSave = $this->image->dirname . "/__" . $this->size . ".$extension"; | ||
|
||
if ($this->size === 'default') { | ||
$pathSave = $this->path; | ||
$this->image->save($pathSave); | ||
}else{ | ||
$this->image->resize($aspectRatio['width'],$aspectRatio['height'])->save($pathSave); | ||
} else { | ||
$aspectRatio = $this->getAspectRatio(); | ||
$this->image->resize($aspectRatio['width'], $aspectRatio['height'])->save($pathSave); | ||
} | ||
} | ||
} | ||
/** | ||
* Check the image's size | ||
* | ||
* @return boolean | ||
*/ | ||
public function checkSize(){ | ||
$result = true; | ||
$widthNew = $this->sizes[$this->size]['width']; | ||
$heightNew = $this->sizes[$this->size]['height'] ; | ||
if ($widthNew >= $this->width && $heightNew >= $this->height ) $result = false; | ||
return $result; | ||
} | ||
|
||
/** | ||
* Check if a specific image exits. | ||
* | ||
* @return boolean | ||
*/ | ||
public function imageExists(){ | ||
$result = false; | ||
$path = $this->path; | ||
if ($this->size !== 'default') { | ||
$path = $this->image->dirname."/__".$this->size.".jpg"; | ||
/** | ||
* Check the image's size | ||
* | ||
* @return boolean | ||
*/ | ||
public function checkSize() | ||
{ | ||
$result = true; | ||
$widthNew = $this->sizes[$this->size]['width']; | ||
$heightNew = $this->sizes[$this->size]['height']; | ||
if ($widthNew >= $this->width && $heightNew >= $this->height) $result = false; | ||
return $result; | ||
} | ||
$result = file_exists($path); | ||
return $result; | ||
} | ||
|
||
/** | ||
* Return an image | ||
* | ||
* @return \Intervention\Image\Image | ||
*/ | ||
public function getImage(){ | ||
$result = $this->image->dirname."/__".$this->size.".jpg"; | ||
if($this->size === "default" ) return $this->image; //$result = $this->path; | ||
return $this->manager->make($result); | ||
} | ||
|
||
private function getAspectRatio($aspectRatio){ | ||
if ($aspectRatio >= 1.0 && $this->height<=$this->sizes[$this->size]['height']) { // Horizontal | ||
$newWidth = $this->sizes[$this->size]['width']; | ||
$newHeight = $newWidth / $aspectRatio; | ||
} else { // Vertical | ||
$newHeight = $this->sizes[$this->size]['height']; | ||
$newWidth = $newHeight * $aspectRatio; | ||
|
||
/** | ||
* Check if a specific image exits. | ||
* | ||
* @return boolean | ||
*/ | ||
public function imageExists(String $extension) | ||
{ | ||
$result = false; | ||
$path = $this->path; | ||
if ($this->size !== 'default') { | ||
$path = $this->image->dirname . "/__" . $this->size . ".$extension"; | ||
} | ||
$result = file_exists($path); | ||
return $result; | ||
} | ||
$result = ["height" => $newHeight,"width" => $newWidth]; | ||
return $result; | ||
} | ||
|
||
public function setSizeDefault(){ | ||
$this->size = 'default'; | ||
} | ||
/** | ||
* Return an image | ||
* | ||
* @return \Intervention\Image\Image | ||
*/ | ||
public function getImage(String $extension) | ||
{ | ||
$result = $this->image->dirname . "/__" . $this->size . ".$extension"; | ||
if ($this->size === "default" || $this->size === "raw") return $this->image; //$result = $this->path; | ||
return $this->manager->make($result); | ||
} | ||
|
||
private function getAspectRatio() | ||
{ | ||
$originalWidth = $this->width; | ||
$originalHeight = $this->height; | ||
|
||
|
||
$targetWidth = $this->sizes[$this->size ]['width']; | ||
$targetHeight = $this->sizes[$this->size ]['height']; | ||
|
||
|
||
$isVertical = $originalHeight > $originalWidth; | ||
|
||
if ($originalWidth <= $targetWidth && $originalHeight <= $targetHeight) { | ||
return ["height" => $originalHeight, "width" => $originalWidth]; | ||
} | ||
|
||
if ($isVertical) { | ||
$newHeight = $targetWidth; | ||
$newWidth = round($newHeight*$originalWidth / $originalHeight); | ||
} else { | ||
$ratio = $originalWidth / $targetWidth; | ||
$newWidth = $targetWidth; | ||
$newHeight = round($originalHeight / $ratio); | ||
} | ||
|
||
return ["height" => $newHeight, "width" => $newWidth]; | ||
} | ||
|
||
public function setSizeDefault() | ||
{ | ||
$this->size = 'default'; | ||
} | ||
|
||
public function pngHasAlpha() | ||
{ | ||
return strpos($this->image->encode('png')->getEncoded(), 'tRNS') !== false; | ||
} | ||
} |
Oops, something went wrong.