Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_errors',1); // enable php error display for easy trouble shooting
- error_reporting(E_ALL); // set error display to all
- /*
- this function intends to reduce the size of image and not for enlargement
- action will be aborted if target file is larger the source
- tested with PHP Version 7.0.13
- */
- class Image {
- private $_logs = null,
- $_quality = [IMAGETYPE_GIF => 0, IMAGETYPE_JPEG => 100, IMAGETYPE_PNG => 9],
- $_methods = [
- IMAGETYPE_GIF => ['imagegif','imagecreatefromgif'],
- IMAGETYPE_JPEG => ['imagejpeg','imagecreatefromjpeg'],
- IMAGETYPE_PNG => ['imagepng','imagecreatefrompng'],
- ];
- private function _checkType() {
- if (
- ($this->_extension == 'image/gif' && $this->_sourceType != IMAGETYPE_GIF) ||
- ($this->_extension == 'image/jpg' && $this->_sourceType != IMAGETYPE_JPEG) ||
- ($this->_extension == 'image/png' && $this->_sourceType != IMAGETYPE_PNG)
- ) {
- $this->_logs[] = "File extension and file type mismatch. Action aborted";
- return false;
- }
- return true;
- }
- private function _load($image) {
- $this->_sourceFile = $image;
- $this->_extension = pathinfo($image)['extension'];
- list($this->_sourceWidth, $this->_sourceHeight, $this->_sourceType) = getimagesize($image);
- $this->_sourceImage = $this->_methods[$this->_sourceType][1]($image);
- }
- private function _createTarget($width, $height) {
- $this->_targetImage = imagecreatetruecolor($width, $height);
- return true;
- }
- private function _output($imageData, $targetPath) {
- imagesavealpha($imageData, true);
- if ($this->_sourceType === IMAGETYPE_GIF || $this->_sourceType === IMAGETYPE_PNG) {
- imagecolortransparent($imageData, $this->_targetTransparency);
- }
- if (!$this->_methods[$this->_sourceType][0]($imageData,$targetPath, $this->_quality[$this->_sourceType])) { "Output failure for {$this->_sourceFile}"; }
- if (!imagedestroy($this->_sourceImage)) { $this->_logs[] = "Unable to destroy temp data for {$this->_sourceFile}"; }
- if (isset($this->_targetImage)) {
- if (!imagedestroy($this->_targetImage)) { $this->_logs[] = "Unable to destroy temp target for {$this->_sourceFile}"; }
- }
- return (!empty($this->_logs))? $this->_logs : true;
- }
- public function resize($image, $thumb, $option = 'maxwidth', $dimension = array('200')) {
- /* validation 1 */
- $options = ['maxwidth','maxheight','contain','cover'];
- if (!in_array($option, $options)) {
- return $this->_logs[] = 'Option Not Defined: maxwidth | maxheight | content | cover. Action aborted';
- }
- /* validation 2 */
- if ($option == 'contain' || $option == 'cover') {
- if (!is_array($dimension) || count($dimension) < 2) {
- return $this->_logs[] = 'Dimensions Not Defined: [width, height]. Action aborted';
- }
- }
- $this->_load($image);
- if (!$this->_checkType()) {
- return false;
- }
- /* validation */
- if (($option == 'maxwidth' && $this->_sourceWidth < $dimension[0]) || ($option == 'maxheight' && $this->_sourceHeight < $dimension[0])) {
- $this->_logs[] = "Target file is larger than source file. Action aborted";
- }
- /* validation */
- if (pathinfo($image)['extension'] != pathinfo($thumb)['extension']) {
- $this->_logs[] = "Target file extension mismatch. Action aborted";
- }
- /* get aspect ratio from source file */
- $ratio = $this->_sourceHeight / $this->_sourceWidth;
- switch ($option) {
- case 'maxwidth':
- $thumbwidth = $dimension[0];
- $thumbheight = $thumbwidth * $ratio;
- break;
- case 'maxheight':
- $thumbheight = $dimension[0];
- $thumbwidth = $thumbheight / $ratio;
- break;
- case 'contain':
- $thumbwidth = $dimension[0];
- $thumbheight = $thumbwidth * $ratio;
- if ($thumbheight > $dimension[1]) {
- $thumbheight = $dimension[1];
- $thumbwidth = $thumbheight / $ratio;
- }
- break;
- case 'cover':
- if ($dimension[0] > $dimension[1]) {
- $thumbwidth = $dimension[0];
- $thumbheight = $thumbwidth * $ratio;
- } else {
- $thumbheight = $dimension[1];
- $thumbwidth = $thumbheight / $ratio;
- }
- break;
- default:
- $thumbwidth = $dimension[0];
- $thumbheight = $thumbwidth * $ratio;
- break;
- }
- /* create new instant */
- $this->_createTarget($thumbwidth, $thumbheight);
- /* $methods indexes based on IMAGETYPE_XXX constants */
- switch ($this->_sourceType) {
- case IMAGETYPE_GIF:
- $this->_targetTransparency = imagecolorallocatealpha($this->_targetImage, 0x7f, 0x7f, 0x7f, 0);
- imagefill($this->_targetImage, 0, 0, $this->_targetTransparency);
- imagecolortransparent($this->_targetImage, $this->_targetTransparency);
- break;
- case IMAGETYPE_JPEG:
- $quality = 100;
- break;
- case IMAGETYPE_PNG:
- imagealphablending($this->_targetImage, false);
- imagesavealpha($this->_targetImage,true);
- $this->_targetTransparency = imagecolorallocatealpha($this->_targetImage, 0, 0, 0, 127);
- // $this->_targetTransparency = imagecolorallocatealpha($this->_targetImage, 255, 255, 255, 127);
- imagefilledrectangle($this->_targetImage, 0, 0, $thumbwidth, $thumbheight, $this->_targetTransparency);
- break;
- default:
- $this->_logs[] = "Unknown type: {$image}";
- break;
- }
- /* create $temp_thumb image */
- imagecopyresampled($this->_targetImage, $this->_sourceImage, 0, 0, 0, 0, $thumbwidth, $thumbheight, $this->_sourceWidth, $this->_sourceHeight);
- if ($option == 'cover') {
- /* calculate offset-x and offset-y for center crop */
- $x = (imagesx($this->_targetImage) - $dimension[0]) / 2;
- $y = (imagesy($this->_targetImage) - $dimension[1]) / 2;
- $x = ($x > 0)? $x : 0; /* probably don't need this */
- $y = ($y > 0)? $y : 0; /* probably don't need this */
- $this->_targetImage = imagecrop($this->_targetImage, ['x' => $x, 'y' => $y, 'width' => $dimension[0], 'height' => $dimension[1]]);
- }
- $this->_output($this->_targetImage, $thumb);
- return (!empty($this->_logs))? $this->_logs : true;
- }
- public function flip ($image, $targetpath, $mode = IMG_FLIP_HORIZONTAL) {
- /* posible $mode = IMG_FLIP_HORIZONTAL | IMG_FLIP_VERTICAL | IMG_FLIP_BOTH */
- $this->_load($image);
- if (!$this->_checkType()) {
- return false;
- }
- imageflip($this->_sourceImage, $mode);
- $this->_createTarget($this->_sourceWidth, $this->_sourceHeight);
- switch ($this->_sourceType) {
- case IMAGETYPE_GIF:
- $this->_targetTransparency = imagecolorallocatealpha($this->_targetImage, 0x7f, 0x7f, 0x7f, 0);
- imagefill($this->_targetImage, 0, 0, $this->_targetTransparency);
- break;
- case IMAGETYPE_JPEG:
- break;
- case IMAGETYPE_PNG:
- break;
- default:
- $this->_logs[] = "Unknown type: {$image}";
- break;
- }
- imagecopyresampled($this->_targetImage, $this->_sourceImage, 0, 0, 0, 0, $this->_sourceWidth, $this->_sourceHeight, $this->_sourceWidth, $this->_sourceHeight);
- $this->_output($this->_sourceImage, $targetpath);
- return (!empty($this->_logs))? $this->_logs : true;
- }
- public function logs() {
- return $this->_logs;
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- .box {
- display: flex;
- align-items: center;
- justify-content: center;
- width: 200px;
- height: 400px;
- border: 1px solid purple;
- }
- </style>
- </head>
- <body>
- <?php
- $image = new Image;
- $source_files = ['test.gif','test.png','test.jpg'];
- foreach ($source_files as $file) {
- $source = 'upload/'.$file;
- $target = 'upload/resize1_'.$file;
- $image->resize($source,$target, 'maxwidth', [400]);
- echo '<div style="background-color: red;"><img src="'.$target.'"></div>';
- }
- foreach ($source_files as $file) {
- $source = 'upload/'.$file;
- $target = 'upload/flip1_'.$file;
- $image->flip($source,$target, IMG_FLIP_HORIZONTAL);
- echo '<div style="background-color: red;"><img src="'.$target.'"></div>';
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement