Jump to content

File extension / image resampling


bebop

Recommended Posts

Hello,

 

I hope you can help me because this piece of script is driving me mad.

 

This script reads a folder, should select files that are of image format - JPG and variants-of-extension, and re-samples & saves them into another folder for thumbnail view.

 

The problem with it is that it works for some jpeg files but not others - indiscriminately! So I have no clue what makes it work with some and not others (in the same folder!!).

 

Then, again - sometimes, if I change a file extension from say "JPEG" to "JPG" and re-run the script, it works on that file (and keeps ignoring the others).

 

If it's of any relevance, all the files come from the same source - the same digital camera.

 

Any clue why?

 

Also one more question if I may, do I need some sort of buffering - sometimes the folder might contain 50 pics (or more) that the script (if working properly) would have to work on...

 

Thank you so much in advance for your kind help!

 

<?php

require_once "database_connection_file.php";

$resampleDir = $_POST['resampleDir'];
$path = $_POST['path'];
$table = $_POST['table'];

if ($handle = opendir($resampleDir)) {
   [b] while (false !== ($file = readdir($handle))) {
        $exts = split("[/\\.]", $file);
        $n = count($exts)-1;
        $exts = $exts[$n];
        if ($exts == "jpg" || $exts == "JPG" || $exts == "JPEG" || $exts == "jpeg") {[/b]
       
        
            $pathToFile = "$resampleDir" . "/";
            $percent = 0.165; // resampling down from original fixed size;

            list($width, $height) = getimagesize($pathToFile . $file);
            $new_width = $width * $percent;
            $new_height = $height * $percent;

            $image_p = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefromjpeg($pathToFile . $file);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
           
            if (!file_exists($path . $file)) {
                $dbPicPath = $path . $file;
                $sql = "INSERT INTO $table (`index`, `name`, `scale`, `rotate`) VALUES (NULL, '$file', NULL, '0')";
                $result = mysql_query($sql, $link) or die(mysql_error());
                imagejpeg($image_p, $path . $file, 100);
                echo "&resampling=" . "yes";
            } else {
                echo "&resampling=" . "no";
            }
        }
    }
    closedir($handle);
}

mysql_close($link);

?>

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.