Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi Larry

 

I thought that I'd post this as it might help other php-ers.

 

I have a website which has many images.  To facilitate the download of images, I save a reduced image under a directory name e.g. images/activities/P3451098.jpg for thumbnails and slideshows as well as the full size in e.g. images/raw/P3451098.jpg.  Then I can offer an enlarged image using <a href = ***><img src...... target = "_blank"> etc.

 

The problem is that as I send the images up using FTP (Filezilla) I can't be sure if all of the reduced images have the corresponding full sized images thanks to advanced dementia.

 

Therefore, I have written a short routine to compare the two directories....

 

COMMENTS:

 

1)  I was confused at first as to why it didn't work properly until I discovered that glob() is case sensitive, so that it wasn't finding .JPGs so had to run it twice (there is a variety of both depending on which camera they came from).

 

2)  The search routine is not very elegant as it runs through all of the raw files even though it may have found a match in the first file.

 

3)  I know that I could use glob(images/raw/*.*) instead of glob(images/raw/*.*) and in this case it wouldn't make any difference as all files in these directories are .jpgs - it just seems more precise to use *.jpg.

 

I would appreciate any comments from anybody.....  

 

 

 





<?php
//Declare variable $found to avoid undefined variable errors
$found = "No";


//Call header which handles security and error messages
include ('adminheader.php');


//Create an array $images which lists all of the relevant image directories on the server..
$images = array('4X4', 'activities', 'almonds', 'alojamiento', 'altiplano', 'archery', 'area', 'artifacts', 'baza', 'castillejar', 'castril', 'caving', 'fiestas', 'flowers', 'history', 'huescar', 'kayaking', 'index', 'markets', 'mountaineering', 'mountains', 'museums', 'photography', 'puebla', 'rafting', 'village', 'wildlife', 'activities/activities', 'activities/caving', 'activities/kayaking', 'activities/mountaineering', 'activities/outdoors', 'activities/rafting');


//Create array $raw which will be a listing of all of the large image files on the server
$raw = glob('images/raw/*.jpg');
$raw2 = glob('images/raw/*.JPG');




//Work through the $images array so as to compare whether there is a large matching file in the 'raw' directory...
foreach($images as $key => $category)
{


//Start with a header to ID each category....
echo 'CATEGORY:...  ' . $category . '<br /><br />';


//Now create a variable ($getlisting) to be able to call only the category that we are interested in...
$getlisting = 'images/' . $category . '/*.jpg';


//create an array of the files in this category..
$listing = glob($getlisting);


//Run through the $raw array to see if it contains a file that matches the file in the category...
foreach($listing as $key => $value)
{


//Create variable to use in str_replace..
$directory = 'images/' . $category . '/';
// Remove the unwanted directory details....
$value = str_replace($directory, "", $value);
//Reduce any JPGs to jpgs...
$value = strtolower($value);


//Run through the $raw array to find a match..
foreach($raw as $key => $rawvalue)
{


//Get rid of directory details from $rawvalue...
$rawvalue = str_replace("images/raw/", "", $rawvalue);
//Bring any .JPGs to .jpgs....
$rawvalue = strtolower($rawvalue);


//Do comparison.  If raw image matches reduced image then do nothing..
if ($value == $rawvalue)
{


//If the small and large files match then switch variable $found to 'on'....
$found = "Found";


}
}
//Run through the $raw array to find a match..
foreach($raw2 as $key => $rawvalue)
{


//Get rid of directory details from $rawvalue...
$rawvalue = str_replace("images/raw/", "", $rawvalue);
//Bring any .JPGs to .jpgs....
$rawvalue = strtolower($rawvalue);


//Do comparison.  If raw image matches reduced image then do nothing..
if ($value == $rawvalue)
{


//If the small and large files match then switch variable $found to 'on'....
$found = "Found";
}
}
//If small and large files don't match, then print out the filename ..
if ($found != "Found")
{
echo $value . "<br /><br />";
}


//reset $found to "Not found"..
$found = "No";
} //Close individual Category  foreach 
echo "<br /><br />";
} //Close all category foreach
echo "ALL DONE!";
include ('footer.php');
?>


 

Link to comment
Share on other sites

Just spotted a problem with the script - it is only checking files with the extension .jpg, not .JPG.  Have changed: 

 

//Now create a variable ($getlisting) to be able to call only the category that we are interested in...
$getlisting = 'images/' . $category . '/*.jpg';

 
to:
 

//Now create a variable ($getlisting) to be able to call only the category that we are interested in...
$getlisting = 'images/' . $category . '/*.*';

 

- can't really see any neat way around it.  I suppose I could run it twice, once with .jpg and then with .JPG, then run the .JPGs through strtolower and then combine them in the $getlisting array to then be compared with  the raw files.

 

Any comments please.

 
Link to comment
Share on other sites

Another question for Larry.

 

I would like to offer a library of all of the thousands of images that I have in my raw data directory by displaying thumbnails.

 

I know I can use: 

<a href = "images/raw/yadayada.jpg" target = "_blank"><img style = "width: 100px;" src = "images/raw/yadayada.jpg" .... /> </a>

but this will involve huge overheads as it will mean downloading approx 1Mb images and then resizing them in the browser, so I would prefer to resize the images in PHP before downloading them, but I can't find any reference to this in the PHP manual, or the PHP GD manual.

 

Any advice would be much appreciated.

 

Max

Edited by Max
Link to comment
Share on other sites

Further to the above comment, I have stumbled across something called 'imagick' which is part of the PHP domain.   

 

The PHP manual says something like this:

bool Imagick::resizeImage ( int $columns , int $rows , int $filter , float $blur [, bool $bestfit = false ] )

..and gives examples like this:

<?php
function resizeImage($imagePath, $width, $height, $filterType, $blur, $bestFit, $cropZoom) {
    //The blur factor where > 1 is blurry, < 1 is sharp.
    $imagick = new \Imagick(realpath($imagePath));

    $imagick->resizeImage($width, $height, $filterType, $blur, $bestFit);

    $cropWidth = $imagick->getImageWidth();
    $cropHeight = $imagick->getImageHeight();

    if ($cropZoom) {
        $newWidth = $cropWidth / 2;
        $newHeight = $cropHeight / 2;

        $imagick->cropimage(
            $newWidth,
            $newHeight,
            ($cropWidth - $newWidth) / 2,
            ($cropHeight - $newHeight) / 2
        );

        $imagick->scaleimage(
            $imagick->getImageWidth() * 4,
            $imagick->getImageHeight() * 4
        );
    }


    header("Content-Type: image/jpg");
    echo $imagick->getImageBlob();
}

?>

...which I can't get to work.

 

Larry - perhaps this could be a subject for your next book?  Last time I suggested a book, you brought out the excellent (but headache inducing :blink: ) Javascript - Develop and Design, which I was pleased to buy but had to read about 5 times for it to start to sink in.  Old dogs and new tricks???  I'm sure it would be a good seller.  You certainly have a way of explaining very complex subjects.

Link to comment
Share on other sites

Hey Max,

 

Sorry for the delayed reply! 

 

First, I'm pretty sure glob() supports regular expressions, so you should be able to write a better pattern matcher. 

 

Second, agreed that you should create thumbnails locally and serve those! imagick is a fine choice for that. You'll need to make sure your PHP installation supports it, of course. 

 

Thanks for the nice words!

Link to comment
Share on other sites

 Share

×
×
  • Create New...