wattee Posted May 25, 2010 Share Posted May 25, 2010 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 More sharing options...
andrewgauger Posted May 25, 2010 Share Posted May 25, 2010 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); } Link to comment https://forums.phpfreaks.com/topic/202812-files-to-directories/#findComment-1063262 Share on other sites More sharing options...
wattee Posted May 26, 2010 Author Share Posted May 26, 2010 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. Link to comment https://forums.phpfreaks.com/topic/202812-files-to-directories/#findComment-1063411 Share on other sites More sharing options...
wattee Posted May 26, 2010 Author Share Posted May 26, 2010 Handled it with some push/pop_array functions and whatnot resulting alot more code but bah nvm thanks though. Link to comment https://forums.phpfreaks.com/topic/202812-files-to-directories/#findComment-1063428 Share on other sites More sharing options...
andrewgauger Posted May 26, 2010 Share Posted May 26, 2010 There is no reason to use a loop to store values so you can reloop through those values. Bad code. Did you even see if my solution worked before you complained and grew a bad problem worse? Link to comment https://forums.phpfreaks.com/topic/202812-files-to-directories/#findComment-1063699 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.