Jump to content

Files to directories


wattee

Recommended Posts

Hi,

bah i keep running to dead ends.

 

I have a function with what i would like to do the following:

I would like to take in a picture object, reduce the size by / 2 with the end of 1x1 pixel img,

create as many folders as there are generated pictures, ie. a pic 1920x1080 would generate folders from 0-10 (calculated by the wider side).

Now with what im stuck with is that it generates 1 picture less. It doesnt generate 1x1 pixel img, and all the files images have shifted, the biggest img is not in 10th folder

but 9th. Here's my code:

 

    function suurus(){
    	
    	        $pic = $this->pilt();
	$korgus = $pic->getImageHeight();
	$pikkus = $pic->getImageWidth();

	for($i=$pikkus; $i>=1; $i=$i-$i/2)		
	{
		$kaustadp[] = $i;
	}

	for($i=$korgus; $i>=1; $i=$i-$i/2)
	{
		$kaustadk[] = $i;
	}

	if ($pikkus > $korgus) {
		$kaustad = $kaustadp;
	} else {
		$kaustad = $kaustadk;
	}


	end($kaustadp);
	end($kaustadk);

	foreach ($kaustad as $nr => $kaust) {			
		if(!file_exists('pildid/test/'.$nr)) {
			mkdir('pildid/test/'.$nr.'/', 0777, true);
		}
		$pic = $this->pilt();

        	$pic->resizeImage(prev($kaustadp), prev($kaustadk), Imagick::FILTER_CATROM,1); 
        	$pic->writeImage('pildid/test/'.$nr.'/bunny.png');			
	}
    }

 

 

Dont mind the language, eh. Im beginning to think that taking params for resizing from an array isn't such a good idea after all, or isit?  Any help is appreciated

Link to comment
https://forums.phpfreaks.com/topic/202812-files-to-directories/
Share on other sites

Instead of multiple loops and for loops I'd just use a while loop.

 

$pic = $this->pilt();
$korgus = $pic->getImageHeight();
$pikkus = $pic->getImageWidth();
$size= ($pikkus > $korgus) ? $pikkus : $korgus;
$nr = 1;

while($size > 0) 
{
if(!file_exists('pildid/test/'.$nr)) {
			mkdir('pildid/test/'.$nr.'/', 0777, true);
		}
		$pic = $this->pilt();

        	$pic->resizeImage(prev($kaustadp), prev($kaustadk), Imagick::FILTER_CATROM,1); 
        	$pic->writeImage('pildid/test/'.$nr.'/bunny.png');			

$size=floor($size/2);
}

 

The 2 for loops are there for creating width array and height array being the parameters for the imageResize.

From foreach loop i get the number of how many folders to create and therefor how many images to create.

 

I dont see how a while loop would solve my problems, or am i missing something here?

 

The problem is that the array values shift down on folders from start 1 (highest folder is empty and the lowest folder has an image wich should be prior to the lowest)  because of the array "prev" function, now what i need to know is how could i assign "max" array value to the resizeImage only the 1st time it loops through and then assign the prev value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.