|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TgUtils; |
| 4 | + |
| 5 | +class ImageUtils { |
| 6 | + |
| 7 | + /** |
| 8 | + * Creates a thumbnail image from the given image at the path. |
| 9 | + * @param string $imagepath - path of original image |
| 10 | + * @param int $maxWidth - maximum new width |
| 11 | + * @param int $maxHeight - maximum new height |
| 12 | + * @param string $targetDir - target directory |
| 13 | + * @return path of the new image file or NULL if it could not be created. |
| 14 | + */ |
| 15 | + public static function createThumbnail($imagePath, $maxWidth, $maxHeight, $targetDir = NULL) { |
| 16 | + $pathinfo = pathinfo($imagePath); |
| 17 | + $targetPath = '.'; |
| 18 | + if ($targetDir == NULL) { |
| 19 | + $targetPath = $pathinfo['dirname'].'/'.$pathinfo['filename'].'.thumbnail.'; |
| 20 | + } else { |
| 21 | + $targetPath = $targetDir.'/'.$pathinfo['filename'].'.thumbnail.'; |
| 22 | + } |
| 23 | + if (class_exists('Imagick')) { |
| 24 | + $image = new \Imagick($imagePath); |
| 25 | + $image->thumbnailImage($maxWidth, $maxHeight, TRUE); |
| 26 | + $targetPath .= 'png'; |
| 27 | + if (!$image->writeImage($targetPath)) { |
| 28 | + $targetPath = NULL; |
| 29 | + } |
| 30 | + } else { |
| 31 | + // We use GD |
| 32 | + $imageDetails = getimagesize($imagePath); |
| 33 | + $image = self::readImage($imagePath, $imageDetails); |
| 34 | + if ($image != NULL) { |
| 35 | + // Compute new dimensions |
| 36 | + $size = self::computeNewSize($imageDetails[0], $imageDetails[1], $maxWidth, $maxHeight); |
| 37 | + |
| 38 | + // Scale it |
| 39 | + $thumbnail = imagecreatetruecolor($size->width, $size-height); |
| 40 | + imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $size->width, $size->height, $imageDetails[0], $imageDetails[1]); |
| 41 | + // Writing it |
| 42 | + $gdInfo = gd_info(); |
| 43 | + // Preferential order PNG, JPEG, WEBP, XPM, WBMP, GIF, BMP |
| 44 | + if ($gdInfo['PNG Support']) { |
| 45 | + $targetPath .= 'png'; |
| 46 | + if (!imagepng($thumbnail, $targetPath)) $targetPath = NULL; |
| 47 | + } else if ($gdInfo['JPEG Support']) { |
| 48 | + $targetPath .= 'jpg'; |
| 49 | + if (!imagejpeg($thumbnail, $targetPath)) $targetPath = NULL; |
| 50 | + } else if ($gdInfo['WebP Support']) { |
| 51 | + $targetPath .= 'webp'; |
| 52 | + if (!imagewebp($thumbnail, $targetPath)) $targetPath = NULL; |
| 53 | + } else if ($gdInfo['XPM Support']) { |
| 54 | + $targetPath .= 'xpm'; |
| 55 | + if (!imagexbm($thumbnail, $targetPath)) $targetPath = NULL; |
| 56 | + } else if ($gdInfo['WBMP Support']) { |
| 57 | + $targetPath .= 'wbmp'; |
| 58 | + if (!imagewbmp($thumbnail, $targetPath)) $targetPath = NULL; |
| 59 | + } else if ($gdInfo['GIF Create Support']) { |
| 60 | + $targetPath .= 'gif'; |
| 61 | + if (!imagegif($thumbnail, $targetPath)) $targetPath = NULL; |
| 62 | + } else if ($gdInfo['BMP Support']) { |
| 63 | + $targetPath .= 'bmp'; |
| 64 | + if (!imagebmp($thumbnail, $targetPath)) $targetPath = NULL; |
| 65 | + } else { |
| 66 | + $targetPath = NULL; |
| 67 | + } |
| 68 | + imagedestroy($thumbnail); |
| 69 | + imagedestroy($image); |
| 70 | + } else { |
| 71 | + $targetPath = NULL; |
| 72 | + } |
| 73 | + } |
| 74 | + return $targetPath; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Reads an image with GD library. |
| 79 | + * @param string $imagePath - where the image is stored. |
| 80 | + * @param array $imageDetails - the result from getimagesize() call when executed before. |
| 81 | + * @return resource form GD library |
| 82 | + */ |
| 83 | + public static function readImage($imagePath, $imageDetails = NULL) { |
| 84 | + if (file_exists($imagePath)) { |
| 85 | + if ($imageDetails == NULL) $imageDetails = getimagesize($imagePath); |
| 86 | + if ($imageDetails !== FALSE) { |
| 87 | + $gdInfo = gd_info(); |
| 88 | + $image = NULL; |
| 89 | + switch ($imageDetails[2]) { |
| 90 | + case IMAGETYPE_BMP: |
| 91 | + if ($gdInfo['BMP Support']) { |
| 92 | + $image = imagecreatefrombmp($imagePath); |
| 93 | + } |
| 94 | + break; |
| 95 | + case IMAGETYPE_GIF: |
| 96 | + if ($gdInfo['GIF Read Support']) { |
| 97 | + $image = imagecreatefromgif($imagePath); |
| 98 | + } |
| 99 | + break; |
| 100 | + case IMG_JPEG: |
| 101 | + case IMG_JPEG: |
| 102 | + if ($gdInfo['JPEG Support']) { |
| 103 | + $image = imagecreatefromjpeg($imagePath); |
| 104 | + } |
| 105 | + break; |
| 106 | + case IMAGETYPE_PNG: |
| 107 | + if ($gdInfo['PNG Support']) { |
| 108 | + $image = imagecreatefrompng($imagePath); |
| 109 | + } |
| 110 | + break; |
| 111 | + case IMAGETYPE_WBMP: |
| 112 | + if ($gdInfo['WBMP Support']) { |
| 113 | + $image = imagecreatefromwbmp($imagePath); |
| 114 | + } |
| 115 | + break; |
| 116 | + case IMG_XPM: |
| 117 | + if ($gdInfo['XPM Support']) { |
| 118 | + $image = imagecreatefromxpm($imagePath); |
| 119 | + } |
| 120 | + break; |
| 121 | + case IMAGETYPE_WEBP: |
| 122 | + if ($gdInfo['WebP Support']) { |
| 123 | + $image = imagecreatefromwebp($imagePath); |
| 124 | + } |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + return $image; |
| 129 | + } |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns an object with width and height attributes to resize. |
| 135 | + * @param int $origWidth - original width |
| 136 | + * @param int $origHeight - original height |
| 137 | + * @param int $maxWidth - maximum new width |
| 138 | + * @param int $maxHeight - maximum new height |
| 139 | + * @return object with width an height attribute |
| 140 | + */ |
| 141 | + public static function computeNewSize($origWidth, $origHeight, $maxWidth, $maxHeight) { |
| 142 | + // Compute new dimensions |
| 143 | + $rc = new \stdClass; |
| 144 | + $rc->width = $maxWidth; |
| 145 | + $rc->height = $maxHeight; |
| 146 | + if ($origWidth < $origHeight) { |
| 147 | + // Portrait |
| 148 | + $rc->width = intval($origWidth * $rc->height / $origHeight); |
| 149 | + } else { |
| 150 | + // Landscape or squared |
| 151 | + $rc->height = intval($origHeight * $rc->width / $origWidth); |
| 152 | + } |
| 153 | + return $rc; |
| 154 | + } |
| 155 | +} |
| 156 | + |
0 commit comments