Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- 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
- */
- function resize($image, $thumb, $option = 'maxwidth', $dimension = array('200')) {
- $methods = [
- IMAGETYPE_GIF => ['imagegif','imagecreatefromgif'],
- IMAGETYPE_JPEG => ['imagejpeg','imagecreatefromjpeg'],
- IMAGETYPE_PNG => ['imagepng','imagecreatefrompng'],
- ];
- $options = ['maxwidth','maxheight','contain','cover'];
- if (!in_array($option, $options)) {
- return $logs[] = 'Option Not Defined: maxwidth | maxheight | content | cover. Action aborted';
- }
- if ($option == 'contain' || $option == 'cover') {
- if (!is_array($dimension) || count($dimension) < 2) {
- return $logs[] = 'Dimensions Not Defined: [width, height]. Action aborted';
- }
- }
- list($source_image_width, $source_image_height, $source_image_type) = getimagesize($image);
- $extension = pathinfo($image)['extension'];
- if (
- ($extension == 'image/gif' && $cource_image_type != IMAGETYPE_GIF) ||
- ($extension == 'image/jpg' && $cource_image_type != IMAGETYPE_JPEG) ||
- ($extension == 'image/png' && $cource_image_type != IMAGETYPE_PNG)
- ) {
- return $logs[] = "File extension and file type mismatch. Action aborted";
- }
- if (($option == 'maxwidth' && $source_image_width < $dimension[0]) || ($option == 'maxheight' && $source_image_height < $dimension[0])) {
- return $logs[] = "Target file is larger than source file. Action aborted";
- }
- if (pathinfo($image)['extension'] != pathinfo($thumb)['extension']) {
- return $logs[] = "Target file extension mismatch. Action aborted";
- }
- $ratio = $source_image_height / $source_image_width;
- 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;
- }
- $temp_thumb = imagecreatetruecolor($thumbwidth, $thumbheight);
- /* $methods indexes based on IMAGETYPE_XXX constants */
- if (!$temp_source = $methods[$source_image_type][1]($image)) { $logs[] = $methods[$source_image_type][1] . 'failure: ' . $image; }
- switch ($source_image_type) {
- case IMAGETYPE_GIF:
- $transparency = imagecolorallocatealpha($temp_thumb, 255, 255, 255, 127);
- imagefilledrectangle($temp_thumb, 0, 0, $thumbwidth, $thumbheight, $transparency);
- break;
- case IMAGETYPE_JPEG:
- break;
- case IMAGETYPE_PNG:
- imagealphablending($temp_thumb, false);
- imagesavealpha($temp_thumb,true);
- $transparency = imagecolorallocatealpha($temp_thumb, 255, 255, 255, 127);
- imagefilledrectangle($temp_thumb, 0, 0, $thumbwidth, $thumbheight, $transparency);
- break;
- default:
- $logs[] = "Unknown type: {$image}";
- break;
- }
- imagecopyresampled($temp_thumb, $temp_source, 0, 0, 0, 0, $thumbwidth, $thumbheight, $source_image_width, $source_image_height);
- if ($option == 'cover') {
- /* calculate offset-x and offset-y for center crop */
- $x = (imagesx($temp_thumb) - $dimension[0]) / 2;
- $y = (imagesy($temp_thumb) - $dimension[1]) / 2;
- $x = ($x > 0)? $x : 0; /* probably don't need this */
- $y = ($y > 0)? $y : 0; /* probably don't need this */
- $temp_thumb = imagecrop($temp_thumb, ['x' => $x, 'y' => $y, 'width' => $dimension[0], 'height' => $dimension[1]]);
- }
- if (!$methods[$source_image_type][0]($temp_thumb,$thumb, 9)) { $logs[] = "Unable to create thumb from temp image of ${image}"; }
- if (!imagedestroy($temp_thumb)) { $log[] = "Unable to destroy temp file for {$image}"; }
- imagedestroy($temp_source);
- return (!empty($logs))? $logs : true;
- }
- ?>
- <!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
- $source_files = ['test.gif','test.png','test.jpg'];
- $dimension = [200, 400];
- foreach ($source_files as $file) {
- $option = 'cover';
- $target_file = 'upload/'.$option.'_'.$file;
- if ($result = resize('upload/' . $file, $target_file, $option, $dimension)) {
- echo '<div class="box"><img src="'.$target_file.'"></div>';
- } else {
- echo '<div class="box">'.implode($result).'</div>';
- }
- }
- echo '<br>';
- foreach ($source_files as $file) {
- $option = 'contain';
- $target_file = 'upload/'.$option.'_'.$file;
- if ($result = resize('upload/' . $file, $target_file, $option, $dimension)) {
- echo '<div class="box"><img src="'.$target_file.'"></div>';
- } else {
- echo '<div class="box">'.implode($result).'</div>';
- }
- }
- echo '<br>';
- foreach ($source_files as $file) {
- $option = 'maxwidth';
- $target_file = 'upload/'.$option.'_'.$file;
- if ($result = resize('upload/' . $file, $target_file, $option, [$dimension[0]])) {
- echo '<div class="box"><img src="'.$target_file.'"></div>';
- } else {
- echo '<div class="box">'.implode($result).'</div>';
- }
- }
- echo '<br>';
- foreach ($source_files as $file) {
- $option = 'maxheight';
- $target_file = 'upload/'.$option.'_'.$file;
- if ($result = resize('upload/' . $file, $target_file, $option, [$dimension[1]])) {
- echo '<div class="box"><img src="'.$target_file.'"></div>';
- } else {
- echo '<div class="box">'.implode($result).'</div>';
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement