Need help with inherited image resize code

Hi guys,

I have been working on a Photo Library project (I have taken over from the original developers). I’ve been working on this a couple of weeks now and I think I hve gone <strike>snow</strike> code blind.

I have the following code but it doesn’t seem to be capable of creating images of a fixed width (with a dynamic height).

	function resizeImage($src, $dest, $maxW, $maxH) {
		$ext = strtolower(substr($src, strlen($src) - 3));
		list($w, $h) = getimagesize($src);

		if(($w < $maxW) or ($h < $maxH)) {
			// If cant resize; then at least copy the *******
			copy($src, $dest);
			return false;
		} else {
			$ratio = $w / $h;
	
			if($ratio > 1) {
				$nW = $maxW;
				$nH = $maxW / $ratio;
			} else {
				$nH = $maxW;
				$nW = $maxW * $ratio;
			}

			$nW = round($nW);
			$nH = round($nH);

			ini_set('memory_limit', '256M');

			switch($ext) {
				case "jpg": $srcImage = imagecreatefromjpeg($src); break;
				case "gif": $srcImage = imagecreatefromgif($src); break;
				case "png": $srcImage = imagecreatefrompng($src); break;
			}

			$destImage = imagecreatetruecolor($nW, $nH);
			imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $nW, $nH, $w, $h);

			switch($ext) {
				case "jpg": imagejpeg($destImage, $dest); break;
				case "gif": imagegif($destImage, $dest); break;
				case "png": imagepng($destImage, $dest); break;
			}

			imagedestroy($destImage);
			imagedestroy($srcImage);
			return true;
		}
	}

If anyone can help, it would be much appreciated and will stop me going insane :frowning:

Thank you for taking the time to take a look.

atw

What exactly is $dest?

Oops, sorry.

$dest is destination (eg. /home/path/to/images/folder)
$src is the source path.
$maxW and $maxH are the maximum width and height respectively (I guess you already knew this, but I added for the sake of clarity). :slight_smile:

atw


if ($maxW > $maxH) {
    $nW = $maxW;
    $nH = ($h * ($maxW / $w));
} else {
    $nW = ($w * ($maxH / $h));
    $nH = $maxH;
}

This is how I’ve always done it, instead of your method of getting the ratio.


##
# Resize.class.php
##
	public function byWidth($width) {
		$newWidth = $width;
		$newHeight = ($this->originalHeight * ($width / $this->originalWidth));
		return $this->resized($newWidth, $newHeight);
	}

	public function byHeight($height) {
		$newWidth = ($this->originalWidth * ($height / $this->originalHeight));
		$newHeight = $height;
		return $this->resized($newWidth, $newHeight);
	}
	
	public function byPercentage($percent) {
		$newWidth = ($this->originalWidth * ($percent / 100));
		$newHeight = ($this->originalHeight * ($percent / 100));
		return $this->resized($newWidth, $newHeight);
	}

	/***/

	private function resized($width, $height) {
		$image = imagecreatetruecolor($width, $height);
		imagecopyresampled($image, $this->image, 0, 0, 0, 0, $width, $height, $this->originalWidth, $this->originalHeight);
		return $image;
	}


try {
	$i = new Resize("./img/back.s1.png");
	# I load the image via createimagefromstring($data) instead of jpeg|png|gif
	# that way I can call imagejpeg|imagepng|imagegif whenever.
	imagejpeg($i->byHeight(100), "./img/thumb/back.s1.jpg", 100);
} catch (Exception $e) {
	echo $e->getMessage();
}

Also, one thing I saw in your function…

$ext = strtolower(substr($src, strlen($src) - 3));

What happens when the extension is .JPEG? That would have to be -4, not -3


$frags = explode(".", $src);
$ext = end($frags);


# the way i go
if (preg_match("/(\\.jpeg|jpg|png|gif)$/i", strtolower($image), $matches)) {
    #checked to see if the supplied filepath is in fact an image.
    $ext = $matches[0]
} else {
    echo "Not an image.";
}