views:

1488

answers:

5

Is there a simple way of dynamically scaling an image in php?

Id like to specifically use some kind of function where i can insert it into my heml such as

<img src=image.php?img=boss.jpg&width=500>

and of course it would then scale the image to whatever height constrains it to 500px wide

i appreciate all input, thanks.

EDIT does need to include jpg png and gif file types

+1  A: 

You could use a GD library and create a simple script that would scale the image as you like. Check the manual

RaYell
+1  A: 

Save this as image.php, it should work like you want it to.

<?php

if (array_key_exists('img', $_GET) === true)
{
    if (is_file($_GET['img']) === true)
    {
     $scale = array();

     $scale[] = (array_key_exists('width', $_GET) === true) ? $_GET['width'] : null;
     $scale[] = (array_key_exists('height', $_GET) === true) ? $_GET['height'] : null;

     Image($_GET['img'], implode('*', $scale));
    }
}

function Image($image, $scale = null)
{
    $type = image_type_to_extension(@exif_imagetype($image), false);

    if (function_exists('ImageCreateFrom' . $type) === true)
    {
     $image = call_user_func('ImageCreateFrom' . $type, $image);

     if (is_resource($image) === true)
     {
      $size = array(ImageSX($image), ImageSY($image));

      if (isset($scale) === true)
      {
       $scale = array_filter(explode('*', $scale), 'is_numeric');

       if (count($scale) >= 1)
       {
        if (empty($scale[0]) === true)
        {
         $scale[0] = $scale[1] * $size[0] / $size[1];
        }

        else if (empty($scale[1]) === true)
        {
         $scale[1] = $scale[0] * $size[1] / $size[0];
        }
       }

       else
       {
        $scale = array($size[0], $size[1]);
       }
      }

      else
      {
       $scale = array($size[0], $size[1]);
      }

      $result = ImageCreateTrueColor($scale[0], $scale[1]);

      if (is_resource($result) === true)
      {
       ImageCopyResampled($result, $image, 0, 0, 0, 0, $scale[0], $scale[1], $size[0], $size[1]);

       if (headers_sent() === false)
       {
        header('Content-Type: image/' . $type);

        if ($type == 'gif')
        {
         return ImageGIF($result, null);
        }

        else if ($type == 'png')
        {
         return ImagePNG($result, null, 9);
        }

        else if ($type == 'jpeg')
        {
         return ImageJPEG($result, null, 90);
        }
       }
      }
     }
    }

    return false;
}

?>
Alix Axel
+1  A: 

not tested

$file = $_GET('img');
$wid = $_GET('width');

// better ways to do this, but this works in a pinch
$orig = @imagecreatefromjpeg($file);
if ($orig === FALSE) $orig = @imagecreatefromgif($file);
if ($orig === FALSE) $orig = @imagecreatefrompng($file);
if ($orig === FALSE) exit("can't continue; $file is unreadable\n");

// aspect ratio stuff
$sx = imagesx($orig);
$sy = imagesy($orig);
$hyt = round($wid * $sy / $sx);

$img = imagecreatetruecolor($wid, $hyt);
imagecopyresampled($img, $orig, 0, 0, 0, 0, $wid, $hyt, $sx, $sy);
header('Content-type: image/jpeg');
imagejpeg($img);
Scott Evernden
+2  A: 

I prefer WideImage library, because it's really really easy to use.

In your case, everything you have to do is:

$img_path = $_GET['img'];
$new_width = $_GET['width'];

$new_img = wiImage::load($img_path)->resize($new_width);

header('Content-Type: image/jpeg');

echo $new_img->asString('jpg', 80);

And it supports jpeg, png, gif, gd, ...

usoban
WideImage is really nice. Image resizing can become pretty complex, using a community tested library for that task is A Good Thing™. :)
deceze
A: 

function save_scaled($img_name,$dst_image,$ext,$max_width,$max_height) { $size=@getimagesize($img_name); $width=$size[0]; $height=$size[1]; if($max_width==0) { $max_width=$width; } if($max_height==0) { $max_height=$height; }

$src=image_create_from($img_name,$ext);

if($width<=$max_width) { if($height<=$max_height) { $tn_width=$width; $tn_height=$height; } else { $th_height=$max_height; $tn_width=round($max_height * $width/$height); } }

else if($height<=$max_height) { $tn_width=$max_width; $tn_height=round($max_width * $height/$width); }

else { $x_ratio=$max_width/$width; $y_ratio=$max_height/$height; } if($x_ratio <=$y_ratio) { $tn_width=$max_width; $tn_height=round($x_ratio*$height); }

else { $tn_height=$max_height; $tn_width=round($y_ratio*$width); }

$dst=@imagecreatetruecolor($tn_width,$th_height);

@imagecopyrcsampled($dst,$src,0,0,0,0,$tn_width,$tn_height,$width,$height);

image_save($dst,$dst_image_name,$ext);

@imagedestroy($src); @imagedestroy($dst); }

govinda
image is not scaling what is the problme pls help urgnt
govinda