[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2009-07-07
2
Medium Priority
?
602 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
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 count occurrences of each item in an array.
Suggested Courses

650 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