SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | ||
| 3 | class Image {
| |
| 4 | private $image; | |
| 5 | private $filename; | |
| 6 | private $width; | |
| 7 | private $height; | |
| 8 | ||
| 9 | public function __construct($filename_with_path) {
| |
| 10 | $this->filename = $filename_with_path; | |
| 11 | $this->image = $this->loadImage(); | |
| 12 | } | |
| 13 | ||
| 14 | /** | |
| 15 | * @param type $save_as_filename Just filename, NOT full path. DO NOT include the file extension, .png will be added automatically. Will be saved to ~/resources/images/profile/[$save_as_filename].png | |
| 16 | */ | |
| 17 | public function saveProfilePicture($save_as_filename) { // Relative filename
| |
| 18 | $filename_with_path = BASE_PATH . '/resources/images/profiles/' . $save_as_filename; | |
| 19 | $this->saveFile($filename_with_path . '.png', 1024, 1024, False); // Save full-size image | |
| 20 | $this->saveFile($filename_with_path . '-32.png', 32, 32, True); | |
| 21 | $this->saveFile($filename_with_path . '-48.png', 48, 48, True); | |
| 22 | $this->saveFile($filename_with_path . '-64.png', 64, 64, True); | |
| 23 | $this->saveFile($filename_with_path . '-128.png', 128, 128, True); | |
| 24 | $this->saveFile($filename_with_path . '-256.png', 256, 256, True); | |
| 25 | } | |
| 26 | ||
| 27 | /** | |
| 28 | * @param type $save_as_filename Just filename, NOT full path. DO NOT include the file extension, .png will be added automatically. Will be saved to ~/resources/images/lists/[$save_as_filename].png | |
| 29 | */ | |
| 30 | public function saveListLogo($save_as_filename) { // Relative filename
| |
| 31 | $filename_with_path = BASE_PATH . '/resources/images/lists/' . $save_as_filename; | |
| 32 | $this->saveFile($filename_with_path . '.png', 1024, 1024, False); // Save full-size image | |
| 33 | $this->saveFile($filename_with_path . '-32.png', 32, 32, False); | |
| 34 | $this->saveFile($filename_with_path . '-48.png', 48, 48, False); | |
| 35 | $this->saveFile($filename_with_path . '-64.png', 64, 64, False); | |
| 36 | $this->saveFile($filename_with_path . '-128.png', 128, 128, False); | |
| 37 | $this->saveFile($filename_with_path . '-256.png', 256, 256, False); | |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * | |
| 42 | * @param type $filename_with_path | |
| 43 | * @param type $width Optional. | |
| 44 | * @param type $height Optional. | |
| 45 | * @param type $crop_to_dimensions Optional. Crops image to the desired dimensions. | |
| 46 | */ | |
| 47 | public function saveFile($filename_with_path, $width, $height, $crop_to_dimensions = False) {
| |
| 48 | $resizedImage = $this->resizeImage($width, $height, $crop_to_dimensions); | |
| 49 | imagePng($resizedImage, $filename_with_path, 9, PNG_ALL_FILTERS); | |
| 50 | } | |
| 51 | ||
| 52 | public function loadImage() {
| |
| 53 | $image_info = getImageSize($this->filename); // http://www.php.net/manual/en/function.getimagesize.php | |
| 54 | $this->width = $image_info[0]; | |
| 55 | $this->height = $image_info[1]; | |
| 56 | ||
| 57 | // Check if there was an error calling getImageSize(): | |
| 58 | if($image_info === False) {
| |
| 59 | throw new Exception('Unable to grab image size');
| |
| 60 | return; | |
| 61 | } | |
| 62 | ||
| 63 | // Define function names for possibly returned mime types | |
| 64 | $image_functions = array( | |
| 65 | IMAGETYPE_GIF => 'imageCreateFromGif', | |
| 66 | IMAGETYPE_JPEG => 'imageCreateFromJpeg', | |
| 67 | IMAGETYPE_PNG => 'imageCreateFromPng', | |
| 68 | IMAGETYPE_WBMP => 'imageCreateFromwBmp', | |
| 69 | IMAGETYPE_XBM => 'imageCreateFromwXbm', | |
| 70 | ); | |
| 71 | ||
| 72 | // Check if there was an error calling getImageSize(): | |
| 73 | if(!function_exists($image_functions[$image_info[2]])) {
| |
| 74 | throw new Exception('Unknown file type');
| |
| 75 | return; | |
| 76 | } | |
| 77 | ||
| 78 | $image = $image_functions[$image_info[2]]($this->filename); | |
| 79 | imageAlphaBlending($image, False); | |
| 80 | imageSaveAlpha($image, True); | |
| 81 | ||
| 82 | return $image; | |
| 83 | } | |
| 84 | ||
| 85 | public function getFileExtension() {
| |
| 86 | $file_extension = pathinfo($this->filename, PATHINFO_EXTENSION); | |
| 87 | return strtolower($file_extension); | |
| 88 | } | |
| 89 | ||
| 90 | function resizeImage($target_width, $target_height, $crop_to_dimensions = False) {
| |
| 91 | //print 'Original: ' . $this->width . 'x' . $this->height . '<br />'; | |
| 92 | ||
| 93 | if($crop_to_dimensions === True) {
| |
| 94 | $width_ratio = $target_width / $this->width; | |
| 95 | //print 'Width Ratio: ' . $width_ratio . '<br />'; | |
| 96 | $height_ratio = $target_height / $this->height; | |
| 97 | //print 'Height Ratio: ' . $height_ratio . '<br />'; | |
| 98 | if($width_ratio > $height_ratio) {
| |
| 99 | //print 'Width Wins<br />'; | |
| 100 | $resized_width = $target_width; | |
| 101 | $resized_height = $this->height * $width_ratio; | |
| 102 | } else {
| |
| 103 | //print 'Height Wins<br />'; | |
| 104 | $resized_height = $target_height; | |
| 105 | $resized_width = $this->width * $height_ratio; | |
| 106 | } | |
| 107 | } else {
| |
| 108 | $width_ratio = $target_width / $this->width; | |
| 109 | //print 'Width Ratio: ' . $width_ratio . '<br />'; | |
| 110 | $resized_width = $target_width; | |
| 111 | $resized_height = $this->height * $width_ratio; | |
| 112 | //print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />'; | |
| 113 | if($resized_height > $target_height) {
| |
| 114 | $height_ratio = $target_height / $resized_height; | |
| 115 | $resized_height = $target_height; | |
| 116 | //print 'Height Ratio: ' . $height_ratio . '<br />'; | |
| 117 | $resized_width = $resized_width * $height_ratio; | |
| 118 | //print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />'; | |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | // Drop decimal values | |
| 123 | $resized_width = round($resized_width); | |
| 124 | $resized_height = round($resized_height); | |
| 125 | ||
| 126 | // Calculations for centering the image | |
| 127 | $offset_width = round(($target_width - $resized_width) / 2); | |
| 128 | $offset_height = round(($target_height - $resized_height) / 2); | |
| 129 | ||
| 130 | //print 'Width Offset: ' . $offset_width . '<br />'; | |
| 131 | //print 'Height Offset: ' . $offset_height . '<br />'; | |
| 132 | ||
| 133 | $new_image = imageCreateTrueColor($target_width, $target_height); | |
| 134 | imageAlphaBlending($new_image, False); | |
| 135 | imageSaveAlpha($new_image, True); | |
| 136 | $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127); | |
| 137 | imagefill($new_image, 0, 0, $transparent); | |
| 138 | ||
| 139 | imageCopyResampled($new_image, $this->image, $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $this->width, $this->height); | |
| 140 | ||
| 141 | //print 'Final: ' . $resized_width . 'x' . $resized_height . '<br /><br /><br />'; | |
| 142 | return $new_image; | |
| 143 | } | |
| 144 | ||
| 145 | function resizeToHeight($original_width, $original_height, $desired_height) {
| |
| 146 | //print 'resize height: ' . $desired_height . '<br />'; | |
| 147 | $ratio = $desired_height / $original_height; | |
| 148 | //print '- ratio: ' . $ratio . '<br />'; | |
| 149 | $width = $original_width * $ratio; | |
| 150 | //print '- new width: ' . $width . '<br />'; | |
| 151 | //print '- new height: ' . $desired_height . '<br />'; | |
| 152 | return array( | |
| 153 | 'width' => $width, | |
| 154 | 'height' => $desired_height | |
| 155 | ); | |
| 156 | } | |
| 157 | ||
| 158 | function resizeToWidth($original_width, $original_height, $desired_width) {
| |
| 159 | $ratio = $desired_width / $original_width; | |
| 160 | $height = $original_height * $ratio; | |
| 161 | return array( | |
| 162 | 'width' => $desired_width, | |
| 163 | 'height' => $height | |
| 164 | ); | |
| 165 | } | |
| 166 | } |