my class sux!!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Post by modplod »

hello again, I'm back with the next problem I have.

I got the class above to function, only I now have to dynamicaly create thumbnails prior to downloading the images to save bandwith.

I have added the following methods to my class to allow it to create thumnails, only its not working right.

For some reason it a: only creates 1 thumbnail that b: it places in the www root and c: is named tn_ with no extention.

Please could someone take a quick look and let me know how to fix it.

here are the methods:

Code: Select all

function makethumbs(){

		$imagefolder=$this->location ."/". $this->galleryName;
		$thumbsdir=$this->location .$this->thumbsFolder.$this->galleryName;

		$fileList = $this->getFileList();
		
		if ($fileList[0]=""){
			echo "no files in directory";
		}else{
			for ($c=0; $c<=sizeof($fileList);$c++){
				$name =$fileList[$c];
				if (!preg_match("/^tn_/",$name)){
					$pics[$c]= $name;
				}
			}
		}

		if ($pics[0]=""){
			echo "no files in directory";
		}else{
			for ($c=0; $c<=sizeof($pics);$c++){
				echo "<br>ok<br>";
				$this->createthumb($pics[$c],"tn_".$pics[$c],150,150);
			}
		}
	}

	function createthumb($name,$filename,$new_w,$new_h)
	{
		$filenameE=explode(".",$name);
		if (preg_match("/jpg|jpeg/",$filenameE[1])){$src_img=imagecreatefromjpeg($name);}
		if (preg_match("/png/",$filenameE[1])){$src_img=imagecreatefrompng($name);}
		$old_x=imageSX($src_img);
		$old_y=imageSY($src_img);
		if ($old_x > $old_y)
		{
			$thumb_w=$new_w;
			$thumb_h=$old_y*($new_h/$old_x);
		}
		if ($old_x < $old_y)
		{
			$thumb_w=$old_x*($new_w/$old_y);
			$thumb_h=$new_h;
		}
		if ($old_x == $old_y)
		{
			$thumb_w=$new_w;
			$thumb_h=$new_h;
		}
		$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
		if (preg_match("/png/",$filenameE[1]))
		{
			imagepng($dst_img,$filename);
		} else {
			imagejpeg($dst_img,$filename);
		}
		imagedestroy($dst_img);
		imagedestroy($src_img);
	}

If it would help I can post a link to a zip of the full working class :lol:
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Code: Select all

imagepng($dst_img,$filename);
        } else {
            imagejpeg($dst_img,$filename);
That would be because here you have specified a file location with $filename. I think you have to leave $filename empty if you don't want it to be saved somewhere. If you want it to dynamically display the image you have to point the <img> at a .php page where the headers are set to show type image/jpeg. The php then returns an image to the page.
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

SOLVED

Post by modplod »

Im not really sure what your getting at neophyte, but I have got this code to place the thumbnails into the correct directory, and create them the right size.

My problem now is that a: the thumbs are all black and b: the script seems to run 1 time too many, and creates a file named tn_ which is also a black thumb, but with no extension.

any ideas?

Code: Select all

function makethumbs(){
	// set the whumbs working dir
	$this->workingdir=$this->location ."/".$this->thumbsFolder."/".$this->galleryName."/";
	// first make sure a dir exists to place the thumbs
	if (!file_exists($workingdir)) {
		mkdir ($workingdir,"0777");
	}

	$fileList = $this->getFileList();


	if ($fileList[0]=""){
		echo "no files in directory";
	}else{
		for ($c=0; $c<=sizeof($fileList);$c++){
			$this->createthumb($fileList[$c],$workingdir."tn_".$fileList[$c],$this->imageMaxWidth,$this->imageMaxHeight);
		}
	}
}

function createthumb($name,$filename,$new_w,$new_h)
{
	$filenameE=explode(".",$name);
	if (preg_match("/jpg|jpeg/",$filenameE[1])){$src_img=imagecreatefromjpeg($name);}
	if (preg_match("/png/",$filenameE[1])){$src_img=imagecreatefrompng($name);}
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	if ($old_x > $old_y)
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y)
	{
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y)
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
	if (preg_match("/png/",$filenameE[1]))
	{
		imagepng($dst_img,$filename);
	} else {
		imagejpeg($dst_img,$filename);
	}
	imagedestroy($dst_img);
	imagedestroy($src_img);
}
Post Reply