| | 68 | |
| | 69 | /** |
| | 70 | * Validates the uploaded file and return a boolean to tell if valid |
| | 71 | * This method should be overridden when you need to write special validation scripts |
| | 72 | * |
| | 73 | * @param string path to input file |
| | 74 | * @param string path to output file (by ref.), making sure you create a struct that matches the $_FILES[][] format |
| | 75 | * |
| | 76 | * @return boolean |
| | 77 | */ |
| | 78 | protected function validateUploadedFile($input, &$output) { |
| | 79 | $errmsg = null; |
| | 80 | // Only if GD is available, resize to max size |
| | 81 | if (Dependencies::check("gd", $errmsg)) { |
| | 82 | // Extract image metadata |
| | 83 | list($width_orig, $height_orig, $type, $attr) = getimagesize($input['tmp_name']); |
| | 84 | // Check if it busts the max size |
| | 85 | if($width_orig > $this->getMaxDisplayWidth() || $height_orig > $this->getMaxDisplayHeight()) { |
| | 86 | // Init with max values |
| | 87 | $width = $this->getMaxDisplayWidth(); |
| | 88 | $height = $this->getMaxDisplayHeight(); |
| | 89 | |
| | 90 | // Compute ratios |
| | 91 | $ratio_orig = $width_orig / $height_orig; |
| | 92 | if ($this->getMaxDisplayWidth() / $this->getMaxDisplayHeight() > $ratio_orig) { |
| | 93 | $width = $height * $ratio_orig; |
| | 94 | } else { |
| | 95 | $height = $width / $ratio_orig; |
| | 96 | } |
| | 97 | |
| | 98 | // Resample |
| | 99 | $image_p = imagecreatetruecolor($width, $height); |
| | 100 | $image = imagecreatefromjpeg($input['tmp_name']); |
| | 101 | imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); |
| | 102 | |
| | 103 | // Build output metadata struct |
| | 104 | $output = array(); |
| | 105 | $output['tmp_name'] = tempnam("/tmp", session_id()); |
| | 106 | $output['type'] = "image/png"; |
| | 107 | $output['name'] = $input['name']; |
| | 108 | |
| | 109 | // Output PNG at full compression (no artefact) |
| | 110 | imagepng($image_p, $output['tmp_name'], 9); |
| | 111 | |
| | 112 | // Write new file size |
| | 113 | $output['size'] = filesize($output['tmp_name']); |
| | 114 | } |
| | 115 | } |
| | 116 | return true; |
| | 117 | } |