Solved

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

Posted on 2009-07-07
2
595 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

691 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