Jump to content

Recommended Posts

Hey everyone,

How can i resize every image in a directory.

What im going to do is have a cron job that runs my file.

The file will resize every image in a given directory to the width 100px x 100px

The reason im doing this is because i dont have control of the images that are uploaded via ftp :(

can anyone help me?

Link to comment
https://forums.phpfreaks.com/topic/97496-image-resize/
Share on other sites

Try this, untested but should be ok, your need to mod it for PNG & GIF, as i pulled the CreateThumb function from an old project and tweeked it a little

 

<?php
$FTP_upload = dirname(__FILE__)."/FTP/";//From Dir
$WWW_Images = dirname(__FILE__)."/images/"; //To Dir

if(!$dh = @opendir($FTP_upload)) return;
while (false !== ($obj = readdir($dh)))
{
        if($obj=='.' || $obj=='..') continue;
        CreateThumb($FTP_upload.'/'.$obj, $WWW_Images.'/'.$obj,100,100)
}
}
closedir($dh);

function CreateThumb($name,$filename,$new_w,$new_h, $Qty=100)
{
ini_set("memory_limit","32M");
if (preg_match('/(?:\.jpg|\.jpeg)$/i', $name)) {
	$src_img=imagecreatefromjpeg($name);
}

if( $src_img === false || $src_img === NULL)
{
	echo "Failed:<br>";
	return false;
}

$old_x=imageSX($src_img);
$old_y=imageSY($src_img);

//dont resize up.
if( ($new_w > $old_x) && ($new_h > $old_y))
{
	$thumb_w=$old_x;
	$thumb_h=$old_y;
}else{
	if ($old_x > $old_y) {
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y) {
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y) {
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}	
}

$dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

imagejpeg($dst_img,$filename, $Qty); 

imagedestroy($dst_img); 
imagedestroy($src_img); 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/97496-image-resize/#findComment-498868
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.