Resize Class

Purpose: Resizes and saves image
Requires : Requires PHP5, GD library.

resize.cls.php

Class resize
		{
			// *** Class variables
		   private $image;
		   private $width;
		   private $height;
		   private $imageResized;

			function __construct($fileName)
			{
				// *** Open up the file
				$this->image = $this->openImage($fileName);

			    // *** Get width and height
			    $this->width  = imagesx($this->image);
			    $this->height = imagesy($this->image);
			}

			## --------------------------------------------------------

			private function openImage($file)
			{
				// *** Get extension
				$extension = strtolower(strrchr($file, '.'));

				switch($extension)
				{
					case '.jpg':
					case '.jpeg':
						$img = @imagecreatefromjpeg($file);
						break;
					case '.gif':
						$img = @imagecreatefromgif($file);
						break;
					case '.png':
						$img = @imagecreatefrompng($file);
						break;
					default:
						$img = false;
						break;
				}
				return $img;
			}

			## --------------------------------------------------------

			public function resizeImage($newWidth, $newHeight, $option="auto")
			{
				// *** Get optimal width and height - based on $option
				$optionArray = $this->getDimensions($newWidth, $newHeight, $option);

				$optimalWidth  = $optionArray['optimalWidth'];
				$optimalHeight = $optionArray['optimalHeight'];


				// *** Resample - create image canvas of x, y size
				$this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
				imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);


				// *** if option is 'crop', then crop too
				if ($option == 'crop') {
					$this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
				}
			}

			## --------------------------------------------------------
			
			private function getDimensions($newWidth, $newHeight, $option)
			{

			   switch ($option)
				{
					case 'exact':
						$optimalWidth = $newWidth;
						$optimalHeight= $newHeight;
						break;
					case 'portrait':
						$optimalWidth = $this->getSizeByFixedHeight($newHeight);
						$optimalHeight= $newHeight;
						break;
					case 'landscape':
						$optimalWidth = $newWidth;
						$optimalHeight= $this->getSizeByFixedWidth($newWidth);
						break;
					case 'auto':
						$optionArray = $this->getSizeByAuto($newWidth, $newHeight);
						$optimalWidth = $optionArray['optimalWidth'];
						$optimalHeight = $optionArray['optimalHeight'];
						break;
					case 'crop':
						$optionArray = $this->getOptimalCrop($newWidth, $newHeight);
						$optimalWidth = $optionArray['optimalWidth'];
						$optimalHeight = $optionArray['optimalHeight'];
						break;
				}
				return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
			}

			## --------------------------------------------------------

			private function getSizeByFixedHeight($newHeight)
			{
				$ratio = $this->width / $this->height;
				$newWidth = $newHeight * $ratio;
				return $newWidth;
			}

			private function getSizeByFixedWidth($newWidth)
			{
				$ratio = $this->height / $this->width;
				$newHeight = $newWidth * $ratio;
				return $newHeight;
			}

			private function getSizeByAuto($newWidth, $newHeight)
			{
				if ($this->height < $this->width)
				// *** Image to be resized is wider (landscape)
				{
					$optimalWidth = $newWidth;
					$optimalHeight= $this->getSizeByFixedWidth($newWidth);
				}
				elseif ($this->height > $this->width)
				// *** Image to be resized is taller (portrait)
				{
					$optimalWidth = $this->getSizeByFixedHeight($newHeight);
					$optimalHeight= $newHeight;
				}
				else
				// *** Image to be resizerd is a square
				{
					if ($newHeight < $newWidth) {
						$optimalWidth = $newWidth;
						$optimalHeight= $this->getSizeByFixedWidth($newWidth);
					} else if ($newHeight > $newWidth) {
						$optimalWidth = $this->getSizeByFixedHeight($newHeight);
						$optimalHeight= $newHeight;
					} else {
						// *** Sqaure being resized to a square
						$optimalWidth = $newWidth;
						$optimalHeight= $newHeight;
					}
				}

				return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
			}

			## --------------------------------------------------------

			private function getOptimalCrop($newWidth, $newHeight)
			{

				$heightRatio = $this->height / $newHeight;
				$widthRatio  = $this->width /  $newWidth;

				if ($heightRatio < $widthRatio) {
					$optimalRatio = $heightRatio;
				} else {
					$optimalRatio = $widthRatio;
				}

				$optimalHeight = $this->height / $optimalRatio;
				$optimalWidth  = $this->width  / $optimalRatio;

				return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
			}

			## --------------------------------------------------------

			private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
			{
				// *** Find center - this will be used for the crop
				$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
				$cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );

				$crop = $this->imageResized;
				//imagedestroy($this->imageResized);

				// *** Now crop from center to exact requested size
				$this->imageResized = imagecreatetruecolor($newWidth , $newHeight);
				imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
			}

			## --------------------------------------------------------

			public function saveImage($savePath, $imageQuality=100)
			{
				// *** Get extension
        		$extension = strrchr($savePath, '.');
       			$extension = strtolower($extension);

				switch($extension)
				{
					case '.jpg':
					case '.jpeg':
						if (imagetypes() & IMG_JPG) {
							imagejpeg($this->imageResized, $savePath, $imageQuality);
						}
						break;

					case '.gif':
						if (imagetypes() & IMG_GIF) {
							imagegif($this->imageResized, $savePath);
						}
						break;

					case '.png':
						// *** Scale quality from 0-100 to 0-9
						$scaleQuality = round(($imageQuality/100) * 9);

						// *** Invert quality setting as 0 is best, not 9
						$invertScaleQuality = 9 - $scaleQuality;

						if (imagetypes() & IMG_PNG) {
							 imagepng($this->imageResized, $savePath, $invertScaleQuality);
						}
						break;

					// ... etc

					default:
						// *** No extension - No save.
						break;
				}

				imagedestroy($this->imageResized);
			}

			## --------------------------------------------------------
		}

index.php

   $resizeObj = new resize('images/cars/large/input.jpg');
   $resizeObj -> resizeImage(150, 100, 0);
   $resizeObj -> saveImage('images/cars/large/output.jpg', 100);

142 thoughts on “Resize Class

  1. Pingback: viagra sales

  2. Pingback: cialis walmart

  3. Pingback: discount cialis

  4. Pingback: cialis 5 mg

  5. Pingback: Viagra australia

  6. Pingback: Brand viagra over the net

  7. Pingback: cialis 5mg

  8. Pingback: cialis pill

  9. Pingback: buying cialis cheap

  10. Pingback: cialis super active

  11. Pingback: cialis online canada

  12. Pingback: online pharmacy viagra

  13. Pingback: online pharmacy viagra

  14. Pingback: viagra 100mg

  15. Pingback: otc ed pills

  16. Pingback: ed meds online without doctor prescription

  17. Pingback: cheap erectile dysfunction

  18. Pingback: pharmacy online

  19. Pingback: online canadian pharmacy

  20. Pingback: cialis visa

  21. Pingback: online vardenafil

  22. Pingback: vardenafil pills

  23. Pingback: vardenafil online pharmacy

  24. Pingback: pala casino online

  25. Pingback: viagra from india

  26. Pingback: best online casino usa

  27. Pingback: real money casino online usa

  28. Pingback: payday loans

  29. Pingback: payday loans online

  30. Pingback: viagra 100mg

  31. Pingback: cialis generic

  32. Pingback: 20 cialis

  33. Pingback: 5 mg cialis

  34. Pingback: cialis 5 mg

  35. Pingback: viagra for women

  36. Pingback: online casino usa real money

  37. Pingback: casino gambling

  38. Pingback: canada viagra

  39. Pingback: cheapest generic viagra

  40. Pingback: cialis online

  41. Pingback: online viagra prescription

  42. Pingback: cialis without prescriptions

  43. Pingback: viagra 100mg tablet price

  44. Pingback: best viagra capsule

  45. Pingback: buy levitra online cheap

  46. Pingback: average cost of viagra

  47. Pingback: buy levitra in uk

  48. Pingback: where to buy viagra uk

  49. Pingback: viagra 1000mg price

  50. Pingback: purchase viagra 100mg

  51. Pingback: 100mg sildenafil 1 pill

  52. Pingback: goodrx cialis 5

  53. Pingback: pfizer viagra liquid

  54. Pingback: cialis price usa

  55. Pingback: cialis

  56. Pingback: https://customessaywriterbyz.com/

  57. Pingback: disertation

  58. Pingback: help with writing essays

  59. Pingback: cheapest essays writing services

  60. Pingback: who can write my paper

  61. Pingback: how to write a phd thesis

  62. Pingback: thesis writing practice

  63. Pingback: buy cheap research papers

  64. Pingback: cialis vasocostrittore

  65. Pingback: order generic cialis online

  66. Pingback: order cialis online us

  67. Pingback: cialis generic name

  68. Pingback: order generic cialis

  69. Pingback: viagra over the counter walmart

  70. Pingback: canada pharmacy no prescription

  71. Pingback: international pharmacy

  72. Pingback: cialis paypal pay with

  73. Pingback: taking cialis soft tabs

  74. Pingback: cialis with dapoxetine from canada

  75. Pingback: viagra cialis enzyte

  76. Pingback: buy cialis philippines

  77. Pingback: find cheap cialis online

  78. Pingback: paypal cialis no prescription

  79. Pingback: buy cialis tadalafil tablets

  80. Pingback: how much does cialis cost with insurance

  81. Pingback: buy cialis online in canada

  82. Pingback: cialis 20mg for sale

  83. Pingback: buy cialis very cheap prices fast delivery

  84. Pingback: hxxxyjiv

  85. Pingback: generic cialis 20 mg tadalafil

  86. Pingback: where can i buy cialis in perth

  87. Pingback: welke viagra beste

  88. Pingback: can you drink alcohol when taking azithromycin

  89. Pingback: taking cialis soft tabs

  90. Pingback: cialis 20mg for sale

  91. Pingback: buy cialis tadalafil tablets

  92. Pingback: genericos de viagra

  93. Pingback: cialis for daily use

  94. Pingback: how to write research statement

  95. Pingback: what should i write my essay on

  96. Pingback: help me write a essay

  97. Pingback: essay topics for business ethics

  98. Pingback: cialis online daily

  99. Pingback: taking cialis soft tabs

  100. Pingback: taking cialis soft tabs

  101. Pingback: augmentin 875 125 mg price

  102. Pingback: lasix 80 mg tablet

  103. Pingback: can you purchase azithromycin over the counter

  104. Pingback: stromectol cream

  105. Pingback: albuterol tablets without prescription

  106. Pingback: milk and doxycycline

  107. Pingback: prednisolone sod phos

  108. Pingback: clomid success rate

  109. Pingback: priligy in israel

  110. Pingback: diflucan for children

  111. Pingback: synthroid 100 mcg

  112. Pingback: help with writing thesis

  113. Pingback: uk dissertation

  114. Pingback: what is thesis in writing

  115. Pingback: custom law essay

  116. Pingback: finasteride proscar propecia

  117. Pingback: neurontin numbness

  118. Pingback: metformin kidney damage

  119. Pingback: paxil 30 mg

  120. Pingback: wine with plaquenil

  121. Pingback: viagra receptfritt

  122. Pingback: prescription drugs without prior prescription

  123. Pingback: side effects of lipitor 40 mg

  124. Pingback: seroquel xr coupons

  125. Pingback: when will generic lyrica be available

  126. Pingback: buy viagra online

  127. Pingback: online generic viagra

  128. Pingback: where to buy sildenafil

  129. Pingback: generic viagra online pharmacy

  130. Pingback: viagra alternatives

  131. Pingback: generic cialis online

  132. Pingback: eli lilly cialis 5mg

  133. Pingback: cialis no prescription

  134. Pingback: cialis germany

  135. Pingback: buying viagra online

  136. Pingback: 1

  137. Pingback: generic viagra online canadian pharmacy

  138. Pingback: cialis price

  139. Pingback: sildenafil 20 mg

  140. Pingback: sildenafil 100mg online india

  141. Pingback: sildenafil 130

  142. Pingback: amoxil tablets 500mg 47

發表迴響