I made a script to resize all the photos in a directory keeping the original photos and the resized pictures keeps the same name as the original photo but with an s characther on the start. Example: original photo - picture.jpg , resized photo: spicture.jpg The resizing and the creation of the new resized picture works ok.
But i am having trouble reading all the photos in the directory and pass to the resize process this is my code :
<?php
$dirpath = "./image";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {
if (!is_dir("$dirpath/$file")) {
echo $file . "<br>"; // Prints the files in the directory
// Get image dimensions
$imagesize = getimagesize($file);
if ($imagesize[0] > 300) {
$newwidth = 183; $newheight = ((183 / $imagesize[0]) * $imagesize[1]);
$image_type = $imagesize[2];
if ($image_type == '2') {
$source = imagecreatefromjpeg($file);
$create = imagejpeg;
}
//image create
$newimage = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $imagesize[0], $imagesize[1]);
// Creates the new resized image file with same name of the Original photo but
//adding an s on the start of the photo name: example: original photo - picture.jpg
//new photo: spicture.jpg
$create($newimage, "s" . $file, 75);
}
} // close if condition
} // close while cicle
closedir($dh);
?>
If someone finds what's wrong let me know
Many thanks.