Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * A class to resize and upload image to a directory.
- */
- class Image_Resize_Upload
- {
- private $imgFilePath;
- private $newWidth;
- private $newHeight;
- private $orgWidth;
- private $orgHeight;
- private $image;
- private $imgType;
- private $imgData;
- private $resizeImg;
- /**
- * Default construct
- * @param Integer - the width to resize image to.
- * @param Integer - the height to resize image to.
- */
- public function __construct( $width, $height = 0 )
- {
- $this->setNewWidth( $width );
- $this->setNewHeight( $height );
- }
- /**
- * sets the image data.
- * @param String the path to the image
- */
- public function setImage( &$image )
- {
- $this->image = $image;
- }
- /**
- * sets the image data.
- * @param String - the path for the file name.
- */
- public function setImgData( &$fileName )
- {
- $this->imgData = getimagesize( $fileName );
- }
- /**
- * sets the image mime type.
- * @param Array - image data.
- */
- public function setImgType( &$imgData )
- {
- $this->imgType = $imgData['mime'];
- }
- /**
- * sets the original width of the image
- * @param String the image data.
- */
- public function setOrgWidth( &$imgData )
- {
- list( $orgWidth, $orgHeight ) = $imgData;
- $this->orgWidth = $orgWidth;
- }
- /**
- * sets the original height of the image.
- * @param String the image data.
- */
- public function setOrgHeight( &$imgData )
- {
- list( $orgWidth, $orgHeight ) = $imgData;
- $this->orgHeight = $orgHeight;
- }
- /**
- * sets the max width
- * @param Integer - image actual width
- */
- public function setNewWidth( &$width )
- {
- $this->newWidth = $width;
- }
- /**
- * sets the max height
- * @param Integer - image actual height
- */
- public function setNewHeight( &$height )
- {
- $this->newHeight = $height;
- }
- /**
- * sets the image file path.
- * @param String - the path where the new image should reside.
- */
- public function setImgPath( $imgFilePath )
- {
- $this->imgFilePath = $imgFilePath;
- }
- /**
- * gets the image data.
- * @return image data.
- */
- public function getImgData()
- {
- return $this->imgData;
- }
- /**
- * gets the image mime type
- * @return image type
- */
- public function getImgType()
- {
- return $this->imgType;
- }
- /**
- * gets the image.
- * @return image.
- */
- public function getImage()
- {
- return $this->image;
- }
- /**
- * gets the new width
- * @return new width
- */
- public function getNewWidth()
- {
- return $this->newWidth;
- }
- /**
- * gets the max height.
- * @return new height.
- */
- public function getNewHeight()
- {
- return $this->newHeight;
- }
- /**
- * gets the path where image should be saved.
- * @return destination path.
- */
- public function getImgPath()
- {
- return $this->imgFilePath;
- }
- /**
- * gets the original image width.
- * @return the original width.
- */
- public function getOrgWidth()
- {
- return $this->orgWidth;
- }
- /**
- * gets the original image height.
- * @return the original height.
- */
- public function getOrgHeight()
- {
- return $this->orgHeight;
- }
- /**
- * gets the maximum allowed image size.
- * @return the maximum size.
- */
- public function getImgMaxSize()
- {
- return $this->$maxFileSize;
- }
- /**
- * resizes image to a new dimension.
- * @param width - the new image width.
- * @param height - the new image height.
- */
- public function resizeImage()
- {
- $this->setOrgWidth( $this->getImgData() );
- $this->setOrgHeight( $this->getImgData() );
- //which height to use
- $height = $this->getNewHeight() == 0 ? $this->calHeight() :
- $this->getNewHeight();
- $this->getImgType();
- $this->createImage();
- $imageNewSize = imagecreatetruecolor( $this->getNewWidth(),
- $height );
- imagecopyresampled( $imageNewSize,
- $this->createImage(), 0, 0, 0, 0,$this->getNewWidth(),
- $height, $this->getOrgWidth(),
- $this->getOrgHeight() );
- $mimeType = $this->getImgType();
- ob_start();
- switch( $mimeType )
- {
- case 'image/png':
- imagepng( $imageNewSize, $this->getImgPath() );
- break;
- case 'image/jpeg':
- imagejpeg( $imageNewSize, $this->getImgPath() );
- break;
- case 'image/gif':
- imagegif( $imageNewSize, $this->getImgPath() );
- break;
- }
- $this->resizedImage = ob_get_contents();
- ob_end_clean();
- return $this->resizedImage;
- }
- /**
- * creates image for resizing.
- */
- public function createImage()
- {
- $mimeType = $this->getImgType();
- switch( $mimeType )
- {
- case 'image/png':
- return imagecreatefrompng(
- $this->getImage() );
- break;
- case 'image/jpeg':
- return imagecreatefromjpeg(
- $this->getImage() );
- break;
- case 'image/gif':
- return imagecreatefromgif(
- $this->getImage() );
- break;
- }
- }
- /**
- * calculates a height for the image to maintain proportionality when
- * resizing the image.
- *
- * @return the new height
- */
- public function calHeight()
- {
- $aspectRatio = ( $this->getOrgHeight() / $this->getOrgWidth() );
- $newHeight = ( $aspectRatio * $this->getNewWidth() );
- return $newHeight;
- }
- /**
- * renames the filename. replaces all white spaces with
- * under scores and change all letters to lowercase.
- * @param String the name of the image.
- *
- * @return replaced all spaces with under scores and all characters
- * are in lowercase.
- */
- public function renameImage( &$filename )
- {
- $newFilename = str_replace( " ", "_",$filename );
- return strtolower( $newFilename );
- }
- /**
- * uploads image after resize
- *
- * @param String - the path where the image resides.
- * @return Integer - 0: file upload succeeded.
- * 1: file is not being upload via HTTP.
- * 2: couldn't copy file to it destination folder.
- */
- public function uploadImage( $sourcePath, $destPath, $fileName )
- {
- $file = $this->renameImage( $fileName );
- $this->setImgPath( $destPath.$file );
- if( is_uploaded_file( $sourcePath ) ) {
- if( copy( $sourcePath, $this->getImgPath() ) ) {
- $this->setImage( $this->getImgPath() );
- $this->setImgData( $this->getImage() );
- $this->setImgType( $this->getImgData() );
- return 0;
- } else {
- return 2;
- }
- } else {
- return 1;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement