Jump to content

Resizing images


Strega

Recommended Posts

Hello,

 

Yesterday I've made this code wich will resize an image 2 times and then puts the original image in another folder.

Album_name

    - originele (folder)

    - resized (folder)

    - thumbs (folder)

    image_1 (image to be converted into the folders and then moved to the originele folder)

    image_2 (...)

 

I tested it a few times and everything worked.

Then I got myself some new pictures and uploaded them the same way I did before.

( I don't remember if I changed something in the script but I don't think so. )

I activated the script and then it went wrong.

The script seems to die in this line :

 

$new_img	=	imagecreatefromjpeg($dir.$file);

 

I know it dies because I've put echo "1"; above and echo "2"; below that line and it only showed 1.

 

Here is the full script, it worked before.

( I've put the HTML tags in it because of the flush() function, I've been told it only works when you put those tags in. )

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl" id="facebook" class=" no_js"> 
<head> 
<title>Resize script</title>
</head> 
<body>
<?php
// Some variables
$dir			=	"../albums/Yamas/";
$ResizedWidth	=	"500";
$ThumbWidth		=	"125";

echo "<b>Dir</b> : ".$dir."<p>\n\n";

// Start
$dir_open	=	opendir($dir);
while (($file = readdir($dir_open)) !== false) {
if($file !== "." && $file !== ".." && $file !== "index.php" && $file !== "thumbs" && $file !== "resized" && $file !== "originele") {
echo "<b>".$file."</b> : Starting to convert from ";
// Check if the file already exists
if(!file_exists($dir."/originele/".$file) && !file_exists($dir."/resized/".$file) && !file_exists($dir."/thumbs/".$file)) {
	// Get the image information
	$info = getimagesize($dir.$file);
	$width	=	$info[0];
	$height	=	$info[1];
	echo "$width x $height<br>\n";

	/////////////////////////////
	// CREATE THE THUMBNAIL //
	////////////////////////////
	echo $dir.$file."<br>\n";
	$new_img	=	imagecreatefromjpeg($dir.$file);
	$imgratio	=	$width/$height;
	$newwidth	=	$ThumbWidth;
	$newheight	=	$ThumbWidth/$imgratio;
	if (function_exists(imagecreatetruecolor)){
		$resized_img	=	imagecreatetruecolor($newwidth,$newheight);
	}else{
		die("Error: Please make sure you have GD library ver 2+");
	}
	// Resize
	imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
	// Save it
	ImageJpeg($resized_img,$dir."thumbs/".$file);
	// Destroy the temp files
	ImageDestroy($resized_img);
	ImageDestroy($new_img);
	echo $file." -> Thumbnail $newwidth x $newheight<br>\n";

	//////////////////////////////
	// CREATE THE RESIZED IMG //
	//////////////////////////////
	// If the current width of the picture is bigger or the same as the maximum width set on top of this document
	if($width >= $ResizedWidth) {
		$new_img	=	imagecreatefromjpeg($dir.$file);
		$imgratio	=	$width/$height;
		$newwidth	=	$ResizedWidth;
		$newheight	=	$ResizedWidth/$imgratio;
		if (function_exists(imagecreatetruecolor)){
			$resized_img	=	imagecreatetruecolor($newwidth,$newheight);
		}else{
			die("Error: Please make sure you have GD library ver 2+");
		}
		// Resize
		imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		// Save it
		ImageJpeg($resized_img,$dir."resized/".$file);
		// Destroy the temp files
		ImageDestroy($resized_img);
		ImageDestroy($new_img);
	}
	echo $file." -> Resized $newwidth x $newheight<br>\n";

	//////////////////////////////
	// MOVE THE ORIGINAL IMG //
	/////////////////////////////
	rename($dir.$file,$dir."originele/".$file);
}
echo "<p>\n\n";
flush();
}
}
closedir($dir_open);
echo "<a href='../?p=albums&album=".$file."'>Bekijk het resultaat</a>";
?>
</body>
</html>

 

Thank you in advance,

Strega

Link to comment
https://forums.phpfreaks.com/topic/180396-resizing-images/
Share on other sites

Hi

 

Are the new images definatly jpegs?

 

Would it be better to change the following line to just specifically check the name for being a jpeg?

 

if($file !== "." && $file !== ".." && $file !== "index.php" && $file !== "thumbs" && $file !== "resized" && $file !== "originele") {

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/180396-resizing-images/#findComment-951684
Share on other sites

Hello,

 

Thanks for the quick reply.

Are the new images definatly jpegs?

Yes they are.

I've also changed this line

if($file !== "." && $file !== ".." && $file !== "index.php" && $file !== "thumbs" && $file !== "resized" && $file !== "originele") {

into

if(preg_match("/.jpg/i",$file)) {

But it still doesn't resize them.

 

Regards, Strega

Link to comment
https://forums.phpfreaks.com/topic/180396-resizing-images/#findComment-951688
Share on other sites

Hi

 

Tried it on my machine and it works. Possibly a problem with one of the image files you have?

 

Only thing is that it seems quite slow (OK, I am using large jpegs), but you could move the code around a bit to improve performance. Ie, not point in checking that the function imagecreatetruecolor exists every time round the loop. Just do it once before bothering to get the directory. Similarly for each file in the directory you load it twice, once as a source for each size.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/180396-resizing-images/#findComment-951695
Share on other sites

Hello,

 

Tried it on my machine and it works. Possibly a problem with one of the image files you have?

 

You are right.

There is something wrong with those files.

When I open them with photoshop it says something with an embedded color profile.

I've just tested it with a jpg file made and saved as jpg on photoshop and that worked.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/180396-resizing-images/#findComment-951717
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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