12:23:57 AM
Thursday
Sep 09
Su M Tu W Th F Sa
567891011
12131415161718
19202122232425
262728293012
3456789

Play My Typing Game

View Some of My Artwork

Determining the dimensions of an image with PHP
<?
$sz = getimagesize("images/some_image.jpg");
echo '<img src="images/some_image.jpg" alt="sample text"';
echo ' width="'.$sz[0].'"';
echo ' height="'.$sz[1].'"';
echo ' />';
?>


now to expand upon the idea, lets say you wanted to resize... and maintain aspect ratio!

<?
$best_width = 75;
$new_height = ceil(abs(($best_width/$sz[0])*$sz[1]));
?>


Here is another example, just to reflect the reverse:
<?
$best_height = 75;
$new_width = ceil(abs(($best_height/$sz[1])*$sz[0]));
?>


Ultimately, here are some functions which resize an image with an optional argument to prevent the image from being too tall or too wide.

<?
function resizetowidth($source_file, $destination_file, $new_width, $max_height='')
{
   list($img_width,$img_height) = getimagesize($source_file); // Get the original dimensions

        if($img_width<=$new_width)
        {
                # copy($source_file, $destination_file);
                $library = "/usr/bin/";
                $magick = "convert -quality 100 -size ". $img_width ."x". $img_height ." ". $source_file ." -resize ". $img_width ."x". $img_height ." +profile \"*\" ". $destination_file;
                $output = system($library . $magick);

        }
        else
        {
                // maintain aspect ratio and find new dimension
                $new_height = ceil(abs(($new_width/$img_width)*$img_height));

                // ensure that this large image fits within max_height optional limitation
                if($max_height!='')
                {
                        if($new_height>$max_height)
                        {
                                $new_height=$max_height;
                                $new_width = ceil(abs(($new_height/$img_height)*$img_width));
                        }
                }

                $library = "/usr/bin/";
                $magick = "convert -quality 100 -size ". $new_width ."x". $new_height ." ". $source_file ." -resize ". $new_width ."x". $new_height ." +profile \"*\" ". $destination_file;
                $output = system($library . $magick);
        }
}

function resizetoheight($source_file, $destination_file, $new_height, $max_width='')
{
   list($img_width,$img_height) = getimagesize($source_file); // Get the original dimensions

        if($img_height<=$new_height)
        {
                # copy($source_file, $destination_file);
                $library = "/usr/bin/";
                $magick = "convert -quality 100 -size ". $img_width ."x". $img_height ." ". $source_file ." -resize ". $img_width ."x". $img_height ." +profile \"*\" ". $destination_file;
                $output = system($library . $magick);

        }
        else
        {
                // maintain aspect ratio and find new dimension
                $new_width = ceil(abs(($new_height/$img_height)*$img_width));

                // ensure that this large image fits within max_width optional limitation
                if($max_width!='')
                {
                        if($new_width>$max_width)
                        {
                                $new_width=$max_width;
                                $new_height = ceil(abs(($new_width/$img_width)*$img_height));
                        }
                }

                $library = "/usr/bin/";
                $magick = "convert -quality 100 -size ". $new_width ."x". $new_height ." ". $source_file ." -resize ". $new_width ."x". $new_height ." +profile \"*\" ". $destination_file;
                $output = system($library . $magick);
        }
}
?>



Read Previous:
Detecting HTTP Headers
Read Next:
Detecting if a .gif image is animated
Table of Contents


Give a listen to my music...
My Best Recordings
How my services and skills can help you:
  • Global (via Email/Skype/AIM(ichat)/Yahoo)
    • Web Development Consulting
    • Custom Web App Development (PHP/MySQL/jQuery/CSS) Cross-Browser & OS
    • Cellphone App Development (Android/MobileWeb)
    • Music Lessons & Education for Beginner Guitar, Piano, Bass, which transfers over to many other instruments I am comfortable playing but not yet at teaching.
    • Music - Soundtrack Composition & Voice Work
  • Local
    • Most all the above, but also:
      • Music Performance (Harp, Voice, Guitar, Piano, Bass, Latin Percussion)
      • Recording Live Music Audio
      • Video Streaming live music and other live events anywhere that can provide wifi
      • Training Video Streaming to your venue's trusted soundperson
Skills:
  • Global (via Email/Skype/AIM(ichat)/Yahoo)
    • Web Development Consulting
    • Custom Web App Development (PHP/MySQL/jQuery/CSS) Cross-Browser & OS
    • Cellphone App Development (Android/MobileWeb)
    • Music Lessons & Education for Beginner Guitar, Piano, Bass, which transfers over to many other instruments I am comfortable playing but not yet at teaching.
    • Music - Soundtrack Composition & Voice Work
  • Local, Most all the above, but also:
    • Music Performance (Harp, Voice, Guitar, Piano, Bass, Latin Percussion)
    • Being your venue's soundman
    • Video Streaming live music and other live events anywhere that can provide wifi
    • Training Video Streaming to your venue's trusted soundperson