| 23 | | protected function set_functions($type) |
| 24 | | { |
| 25 | | static $imagecreate; |
| 26 | | static $imagesave; |
| 27 | | |
| 28 | | if ($imagecreate === NULL) |
| 29 | | { |
| 30 | | $imagecreate = array |
| 31 | | ( |
| 32 | | 1 => 'imagecreatefromgif', |
| 33 | | 2 => 'imagecreatefromjpeg', |
| 34 | | 3 => 'imagecreatefrompng' |
| 35 | | ); |
| 36 | | |
| 37 | | $imagesave = array |
| 38 | | ( |
| 39 | | 1 => 'imagegif', |
| 40 | | 2 => 'imagejpeg', |
| 41 | | 3 => 'imagepng' |
| 42 | | ); |
| 43 | | } |
| 44 | | |
| 45 | | // Set the create function |
| 46 | | isset($imagecreate[$type]) |
| 47 | | and function_exists($imagecreate[$type]) |
| 48 | | and $this->imagecreate = $imagecreate[$type]; |
| 49 | | |
| 50 | | // Set the save function |
| 51 | | isset($imagesave[$type]) |
| 52 | | and function_exists($imagesave[$type]) |
| 53 | | and $this->imagesave = $imagesave[$type]; |
| 54 | | |
| 55 | | return ! (empty($this->imagecreate) OR empty($this->imagesave)); |
| 56 | | } |
| 57 | | |
| 60 | | // Make sure the image type is supported |
| 61 | | if ( ! $this->set_functions($image['type'])) |
| | 19 | switch ($image['type']) |
| | 20 | { |
| | 21 | case IMAGETYPE_JPEG: |
| | 22 | $create = 'imagecreatefromjpeg'; |
| | 23 | break; |
| | 24 | case IMAGETYPE_GIF: |
| | 25 | $create = 'imagecreatefromgif'; |
| | 26 | break; |
| | 27 | case IMAGETYPE_PNG: |
| | 28 | $create = 'imagecreatefrompng'; |
| | 29 | break; |
| | 30 | } |
| | 31 | |
| | 32 | switch (substr($file, strrpos($file, '.') + 1)) |
| | 33 | { |
| | 34 | case 'jpg': |
| | 35 | case 'jpeg': |
| | 36 | $save = 'imagejpeg'; |
| | 37 | break; |
| | 38 | case 'gif': |
| | 39 | $save = 'imagegif'; |
| | 40 | break; |
| | 41 | case 'png': |
| | 42 | $save = 'imagepng'; |
| | 43 | break; |
| | 44 | } |
| | 45 | |
| | 46 | // Make sure the image type is supported for import |
| | 47 | if (empty($create) OR ! function_exists($create)) |
| 75 | | // Image save function alias |
| 76 | | $save = $this->imagesave; |
| 77 | | |
| 78 | | // Save the image to set the status |
| 79 | | $status = $save($this->tmp_image, $dir.$file); |
| | 65 | // Prevent the alpha from being lost |
| | 66 | imagealphablending($this->tmp_image, TRUE); |
| | 67 | imagesavealpha($this->tmp_image, TRUE); |
| | 68 | |
| | 69 | switch ($save) |
| | 70 | { |
| | 71 | case 'imagejpeg': |
| | 72 | if ($quality === NULL) |
| | 73 | { |
| | 74 | // Default quality is 95% |
| | 75 | $quality = 95; |
| | 76 | } |
| | 77 | else |
| | 78 | { |
| | 79 | // Make sure the quality is in range |
| | 80 | $quality = max(1, min($quality, 100)); |
| | 81 | } |
| | 82 | break; |
| | 83 | case 'imagegif': |
| | 84 | // Remove the quality setting, GIF doesn't use it |
| | 85 | unset($quality); |
| | 86 | break; |
| | 87 | case 'imagepng': |
| | 88 | // Always use a compression level of 9 for PNGs. |
| | 89 | // This does not affect quality, it only increases the level of compression! |
| | 90 | $quality = 9; |
| | 91 | break; |
| | 92 | } |
| | 93 | |
| | 94 | // Set the status to the save return value, saving with the quality reques |
| | 95 | $status = isset($quality) ? $save($this->tmp_image, $dir.$file, $quality) : $save($this->tmp_image, $dir.$file); |
| 105 | | imagecopyresampled($tmp, $this->tmp_image, 0, 0, $properties['left'], $properties['top'], $width, $height, $width, $height); |
| 106 | | |
| 107 | | // Destroy the temporary image |
| 108 | | imagedestroy($this->tmp_image); |
| 109 | | |
| 110 | | // Set the temporary image to this image |
| 111 | | $this->tmp_image = $tmp; |
| 112 | | |
| 113 | | return TRUE; |
| | 121 | if ($status = imagecopyresampled($img, $this->tmp_image, 0, 0, $properties['left'], $properties['top'], $width, $height, $width, $height)) |
| | 122 | { |
| | 123 | // Swap the new image for the old one |
| | 124 | imagedestroy($this->tmp_image); |
| | 125 | $this->tmp_image = $img; |
| | 126 | } |
| | 127 | |
| | 128 | return $status; |