开发者

image resize with php?

开发者 https://www.devze.com 2023-04-07 05:58 出处:网络
ive done my research on the net and here as well and i keep on coming up with no answer that can help me:

ive done my research on the net and here as well and i keep on coming up with no answer that can help me: i have the below code which displays an image from a folder based on a user's location however the image is too big and i need to resize it. all the scripts that i have tried or read relate to files being uploaded. can anyone push me in the right direction?

thank you.

 <?php


        print"  

        <table  <td width=\"138\" height=\"73\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
      <tr>
        <td align=\"center\" valign=\"middle\"><a href=\"map.php\"><img src=\"" .
         esc('img/' . $db_name . '_maps/sm' . $us开发者_如何学Goer['location'] . '.png') .
         "\" alt=\"Map of systems around {$user['location']}\" /></a></td>
      </tr>
    </table>
         "
    ?>

My problem arises from the fact that i need to pull the images as:

<img src=\"" .esc('img/' . $db_name . '_maps/sm' . $user['location'] . '.png') . "\" alt=\"Map of systems around {$user['location']}\" /></a>


Wrote a tutorial about this a while ago. Perhaps it can help. It starts with uploading, but most of it is about resizing. Just swap out the usage of the $_FILES array by geting the image type and file name a different way. Here's the code you should need:

// Create image from file
switch(strtolower($_FILES['image']['type']))
{
     case 'image/jpeg':
         $image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
         break;
     case 'image/png':
         $image = imagecreatefrompng($_FILES['image']['tmp_name']);
         break;
     case 'image/gif':
         $image = imagecreatefromgif($_FILES['image']['tmp_name']);
         break;
     default:
         exit('Unsupported type: '.$_FILES['image']['type']);
}

// Target dimensions
$max_width = 240;
$max_height = 180;

// Get current dimensions
$old_width  = imagesx($image);
$old_height = imagesy($image);

// Calculate the scaling we need to do to fit the image inside our frame
$scale      = min($max_width/$old_width, $max_height/$old_height);

// Get the new dimensions
$new_width  = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);

// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);

// Resize old image into new
imagecopyresampled($new, $image, 
     0, 0, 0, 0, 
     $new_width, $new_height, $old_width, $old_height);

// Catch the imagedata
ob_start();
imagejpeg($new, NULL, 90);
$data = ob_get_clean();

// Destroy resources
imagedestroy($image);
imagedestroy($new);

// Set new content-type and status code
header("Content-type: image/jpeg", true, 200);

// Output data
echo $data;

If you want to store the image as a file rather than dumping it to the browser, remove the head and echo part at the end and then swap out the NULL parameter in the imagejpeg call with an actual filename. Hope that helps :)

Here's the code in use: http://samples.geekality.net/image-resize/


You make take a look at gd : http://www.php.net/manual/en/function.imagecopyresized.php

You can try this:

$extension = substr( $img_url, -3 );
$extension = strtolower($extension);
switch ($extension) {
case "jpg":
case "jpeg":
$src_im = createimagefromjpeg($img_url);
break;
case "gif":
$src_im = createimagefromgif($img_url);
break;
case "png":
$src_im = createimagefrompng($img_url);
break;
}
// Get size
$size = GetImageSize($img_url);
$src_w = $size[0];
$src_h = $size[1];

// $width has to be fixed to your wanted width
$dst_w = $width;
$dst_h = round(($dst_w / $src_w) * $src_h);
$dst_im = ImageCreateTrueColor($dst_w, $dst_h);
ImageCopyResampled($dst_im, $src_im, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
ImageJpeg($dst_im);
ImageDestroy($dst_im);
imageDestroy($src_im);


There a simple to use, open source library called PHP Image Magician that has some nice features and documentation.

It uses 100% GD.

Example of basis usage:

$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');


If you need more features convert might be a help.

0

精彩评论

暂无评论...
验证码 换一张
取 消