Jump to content

[SOLVED] Thumbnail script working but still creating errors??


farkewie

Recommended Posts

Hi i have created a thumbnail script seems to be working but it still leaves errors.

i know its working because i have 30 images in my test folder and i get 30 thumbs.

 

code:

 

<?php
$dir = "images";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
   $files[] = $filename;

}

sort($files);


rsort($files);
$dir = "images/";
if (!file_exists($dir."/thumbs")){
mkdir($dir."/thumbs");
}

if (!file_exists($dir."/thumbs".$files)){
foreach ($files as $files) {





$minwidth = "150";
$minheight = "150";

$photosize = getimagesize($dir.$files);
	    // Get image size and scale ratio
	    $scale = min("150"/$photosize[0], "150"/$photosize[1]);
		if ($scale < 1) {
		   $width = floor($scale*$photosize[0]);
		   $height = floor($scale*$photosize[1]);
		}
		else {
		   $width = $minwidth;
		   $height = $minheight;
		}
		if ($photosize['mime']=="image/jpeg") {
			$resizedimage = imagecreatetruecolor($width, $height);
			$thumbimage = imagecreatefromjpeg($dir.$files);
			imagecopyresampled($resizedimage, $thumbimage, 0, 0, 0, 0, $width, $height, $photosize[0], $photosize[1]);
			imagejpeg($resizedimage,$dir."/thumbs/THUMBS_".$files,100);
			imageDestroy($resizedimage); 
			imageDestroy($thumbimage); 
		}
	}


}



?>

 

Error:

 

 

 

Warning: getimagesize(images/thumbs) [function.getimagesize]: failed to open stream: Permission denied in Z:\www\htdocs\personal\test.php on line 28

 

Warning: Division by zero in Z:\www\htdocs\personal\test.php on line 30

 

Warning: Division by zero in Z:\www\htdocs\personal\test.php on line 30

 

Warning: getimagesize(images/..) [function.getimagesize]: failed to open stream: Permission denied in Z:\www\htdocs\personal\test.php on line 28

 

Warning: Division by zero in Z:\www\htdocs\personal\test.php on line 30

 

Warning: Division by zero in Z:\www\htdocs\personal\test.php on line 30

 

Warning: getimagesize(images/.) [function.getimagesize]: failed to open stream: Permission denied in Z:\www\htdocs\personal\test.php on line 28

 

Warning: Division by zero in Z:\www\htdocs\personal\test.php on line 30

 

Warning: Division by zero in Z:\www\htdocs\personal\test.php on line 30

 

Link to comment
Share on other sites

try this modified code

 

<?php
$dir = "images";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
   $files[] = $filename;

}

sort($files);


rsort($files);
$dir = "images/";
if (!file_exists($dir."/thumbs")){
mkdir($dir."/thumbs");
}

foreach ($files as $file) {

if (!file_exists($dir."/THUMBS_".$file)){




$minwidth = "150";
$minheight = "150";

$photosize = getimagesize($dir.$file);
	    // Get image size and scale ratio
	    $scale = min("150"/$photosize[0], "150"/$photosize[1]);
		if ($scale < 1) {
		   $width = floor($scale*$photosize[0]);
		   $height = floor($scale*$photosize[1]);
		}
		else {
		   $width = $minwidth;
		   $height = $minheight;
		}
		if ($photosize['mime']=="image/jpeg") {
			$resizedimage = imagecreatetruecolor($width, $height);
			$thumbimage = imagecreatefromjpeg($dir.$file);
			imagecopyresampled($resizedimage, $thumbimage, 0, 0, 0, 0, $width, $height, $photosize[0], $photosize[1]);
			imagejpeg($resizedimage,$dir."/thumbs/THUMBS_".$file,100);
			imageDestroy($resizedimage); 
			imageDestroy($thumbimage); 
		}
	}


}

 

 

?>

 

Link to comment
Share on other sites

Ok so i found what the problem is i just dont know how to fix it

 

Code:

 

<?php
print_r($files);
?>

 

Returns.

 

Array

(

    [0] => thumbs

    [1] => image021.jpg

    [2] => image020.jpg

    [3] => image019.jpg

    [4] => image018.jpg

    [5] => image017.jpg

    [6] => image016.jpg

    [7] => image015.jpg

    [8] => image014.jpg

    [9] => image013.jpg

    [10] => image012.jpg

    [11] => image011.jpg

    [12] => image010.jpg

    [13] => image009.jpg

    [14] => image008.jpg

    [15] => image007.jpg

    [16] => image006.jpg

    [17] => image005.jpg

    [18] => image004.jpg

    [19] => image003.jpg

    [20] => image002.jpg

    [21] => image001.jpg

    [22] => ..

    [23] => .

)

 

 

 

notice the dots don the bottom? how do i exlude them from my thumbnail creation?

Link to comment
Share on other sites

Putting this in your while loop will skip the dots:

 

if($filename == '.' || $filename == '..'){continue;}

 

Like this i guess:

 

while (false !== ($filename = readdir($dh))) {
if($filename == '.' || $filename == '..'){continue;}
$files[] = $filename;
}

 

EDIT: I had the variable names wrong at first, just fixed them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.