Solved

PHP quesiton - need to adjust createthumb() function to scale / resize based on width

Posted on 2009-07-07
2
584 Views
Last Modified: 2013-12-13
I need to modify a PHP script so that the generated thumbnail images are scaled / resized based on the width (see attached script).

I normally call the function as follows:

createthumb($filePath,$thumbnail,165,165);

However, .. what ends up happening is that when I upload images that are in portrait rather than landscape mode -- the generated thumbnails are scaled based on the height -- meaning that the height is 165px, .. and the width is scaled proportionally relative to that. In need to somehow update the script so that it does the opposite -- where the height scales relative to the forced 165px width.  Please advise on how I might accomplish this.

Thanks,
- Yvan




<?php 

$imagefolder='.';

$thumbsfolder='.';

$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");

$pics=ditchtn($pics,"tn_");

if ($pics[0]!="")

{

	foreach ($pics as $p)

	{

		createthumb($p,"tn_".$p,150,150);

	}

}
 

/*

	Function ditchtn($arr,$thumbname)

	filters out thumbnails

*/

function ditchtn($arr,$thumbname)

{

	foreach ($arr as $item)

	{

		if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}

	}

	return $tmparr;

}
 

/*

	Function createthumb($name,$filename,$new_w,$new_h)

	creates a resized image

	variables:

	$name		Original filename

	$filename	Filename of the resized image

	$new_w		width of resized image

	$new_h		height of resized image

*/	

function createthumb($name,$filename,$new_w,$new_h)

{

	$system=explode(".",$name);

	if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}

	if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}

	$old_x=imageSX($src_img);

	$old_y=imageSY($src_img);

	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); 

	if (preg_match("/png/",$system[1]))

	{

		imagepng($dst_img,$filename); 

	} else {

		imagejpeg($dst_img,$filename); 

	}

	imagedestroy($dst_img); 

	imagedestroy($src_img); 

}
 

/*

        Function directory($directory,$filters)

        reads the content of $directory, takes the files that apply to $filter 

		and returns an array of the filenames.

        You can specify which files to read, for example

        $files = directory(".","jpg,gif");

                gets all jpg and gif files in this directory.

        $files = directory(".","all");

                gets all files.

*/

function directory($dir,$filters)

{

	$handle=opendir($dir);

	$files=array();

	if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}

	if ($filters != "all")

	{

		$filters=explode(",",$filters);

		while (($file = readdir($handle))!==false)

		{

			for ($f=0;$f<sizeof($filters);$f++):

				$system=explode(".",$file);

				if ($system[1] == $filters[$f]){$files[] = $file;}

			endfor;

		}

	}

	closedir($handle);

	return $files;

}

?>

Open in new window

0
Comment
Question by:egoselfaxis
2 Comments
 
LVL 10

Accepted Solution

by:
Chad Smith earned 500 total points
ID: 24797669

<?php 

$imagefolder='.';

$thumbsfolder='.';

$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");

$pics=ditchtn($pics,"tn_");

if ($pics[0]!="")

{

        foreach ($pics as $p)

        {

                createthumb($p,"tn_".$p,150,150);

        }

}

 

/*

        Function ditchtn($arr,$thumbname)

        filters out thumbnails

*/

function ditchtn($arr,$thumbname)

{

        foreach ($arr as $item)

        {

                if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}

        }

        return $tmparr;

}

 

/*

        Function createthumb($name,$filename,$new_w,$new_h)

        creates a resized image

        variables:

        $name           Original filename

        $filename       Filename of the resized image

        $new_w          width of resized image

        $new_h          height of resized image

*/      

function createthumb($name,$filename,$new_w,$new_h)

{

        $system=explode(".",$name);

        if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}

        if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}

        $old_x=imageSX($src_img);

        $old_y=imageSY($src_img);

        if ($old_x == $old_y) 

        {

                $thumb_w=$new_w;

                $thumb_h=$new_h;

        }

	else 

	{

                $thumb_w=$new_w;

                $thumb_h=$old_y*($new_h/$old_x);

	}

        $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

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

        if (preg_match("/png/",$system[1]))

        {

                imagepng($dst_img,$filename); 

        } else {

                imagejpeg($dst_img,$filename); 

        }

        imagedestroy($dst_img); 

        imagedestroy($src_img); 

}

 

/*

        Function directory($directory,$filters)

        reads the content of $directory, takes the files that apply to $filter 

                and returns an array of the filenames.

        You can specify which files to read, for example

        $files = directory(".","jpg,gif");

                gets all jpg and gif files in this directory.

        $files = directory(".","all");

                gets all files.

*/

function directory($dir,$filters)

{

        $handle=opendir($dir);

        $files=array();

        if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}

        if ($filters != "all")

        {

                $filters=explode(",",$filters);

                while (($file = readdir($handle))!==false)

                {

                        for ($f=0;$f<sizeof($filters);$f++):

                                $system=explode(".",$file);

                                if ($system[1] == $filters[$f]){$files[] = $file;}

                        endfor;

                }

        }

        closedir($handle);

        return $files;

}

?>

Open in new window

0
 

Author Comment

by:egoselfaxis
ID: 24797776
Works great!  Thanks!

- yg
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now