diff options
Diffstat (limited to 'protected/extensions/SimpleImage/SimpleImage.php')
| -rw-r--r-- | protected/extensions/SimpleImage/SimpleImage.php | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/protected/extensions/SimpleImage/SimpleImage.php b/protected/extensions/SimpleImage/SimpleImage.php new file mode 100644 index 0000000..bbccfa0 --- /dev/null +++ b/protected/extensions/SimpleImage/SimpleImage.php @@ -0,0 +1,151 @@ +<?php
+/**
+ * Class to hold information about an image and to provide basic operations on images.
+ * @link http://www.yiiframework.com/extension/simpleimage/
+ * @author tzur
+ *
+ */
+class SimpleImage {
+ /**
+ * The image.
+ * @var resource
+ */
+ var $image;
+ /**
+ * The type of the image.
+ * @see http://www.php.net/manual/en/image.constants.php
+ * @var integer
+ */
+ var $image_type;
+ /**
+ * The image informations.
+ * @see http://www.php.net/manual/en/function.getimagesize.php
+ * @var array
+ */
+ var $image_info;
+
+ /**
+ * Creates the SimpleImage instance and loads all necessary information.
+ * @param string $filename
+ * @link http://www.php.net/manual/en/function.getimagesize.php
+ * @link http://www.php.net/manual/en/function.imagecreatefromjpeg.php
+ * @link http://www.php.net/manual/en/function.imagecreatefromgif.php
+ * @link http://www.php.net/manual/en/function.imagecreatefrompng.php
+ *
+ */
+ function __construct($filename) {
+ $this->image_info = $image_info = getimagesize($filename);
+ $this->image_type = $image_info[2];
+ if ($this->image_type == IMAGETYPE_JPEG) {
+ $this->image = imagecreatefromjpeg($filename);
+ } elseif ($this->image_type == IMAGETYPE_GIF) {
+ $this->image = imagecreatefromgif($filename);
+ } elseif ($this->image_type == IMAGETYPE_PNG) {
+ $this->image = imagecreatefrompng($filename);
+ }
+ }
+
+ /**
+ * Saves the image.
+ * @param string $filename
+ * @param int $image_type
+ * @param int $compression
+ * @param int $permissions
+ * @link http://www.php.net/manual/en/function.imagejpeg.php
+ * @link http://www.php.net/manual/en/function.imagegif.php
+ * @link http://www.php.net/manual/en/function.imagepng.php
+ * @link http://www.php.net/manual/en/function.chmod.php
+ */
+ function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = null) {
+ if ($image_type == IMAGETYPE_JPEG) {
+ imagejpeg($this->image, $filename, $compression);
+ } elseif ($image_type == IMAGETYPE_GIF) {
+ imagegif($this->image, $filename);
+ } elseif ($image_type == IMAGETYPE_PNG) {
+ imagepng($this->image, $filename);
+ }
+ if ($permissions != null) {
+ chmod($filename, $permissions);
+ }
+ }
+
+ /**
+ * Outputs the image.
+ * @param int $image_type
+ * @link http://www.php.net/manual/en/function.imagejpeg.php
+ * @link http://www.php.net/manual/en/function.imagegif.php
+ * @link http://www.php.net/manual/en/function.imagepng.php
+ */
+ function output($image_type = IMAGETYPE_JPEG) {
+ if ($image_type == IMAGETYPE_JPEG) {
+ imagejpeg($this->image);
+ } elseif ($image_type == IMAGETYPE_GIF) {
+ imagegif($this->image);
+ } elseif ($image_type == IMAGETYPE_PNG) {
+ imagepng($this->image);
+ }
+ }
+
+ /**
+ * Returns the width of the image.
+ * @return int Return the width of the image or false on errors.
+ * @link http://www.php.net/manual/en/function.imagesx.php
+ */
+ function getWidth() {
+ return imagesx($this->image);
+ }
+
+ /**
+ * Returns the height of the image.
+ * @return int Return the height of the image or false on errors.
+ * @link http://www.php.net/manual/en/function.imagesy.php
+ */
+ function getHeight() {
+ return imagesy($this->image);
+ }
+
+ /**
+ * Resizes the image to given height.
+ * Keeps the ratio.
+ * @param number $height
+ */
+ function resizeToHeight($height) {
+ $ratio = $height / $this->getHeight();
+ $width = $this->getWidth() * $ratio;
+ $this->resize($width, $height);
+ }
+
+ /**
+ * Resizes the image to given width.
+ * Keeps the ratio.
+ * @param number $width
+ */
+ function resizeToWidth($width) {
+ $ratio = $width / $this->getWidth();
+ $height = $this->getHeight() * $ratio;
+ $this->resize($width, $height);
+ }
+
+ /**
+ * Scales the image with given scale.
+ * @param number $scale
+ */
+ function scale($scale) {
+ $width = $this->getWidth() * $scale / 100;
+ $height = $this->getHeight() * $scale / 100;
+ $this->resize($width, $height);
+ }
+
+ /**
+ * Resizes the image to given width and height.
+ * @param number $width
+ * @param number $height
+ * @link http://www.php.net/manual/en/function.imagecopyresampled.php
+ */
+ function resize($width, $height) {
+ $new_image = imagecreatetruecolor($width, $height);
+ imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
+ $this->image = $new_image;
+ }
+}
+?>
\ No newline at end of file |
