Resizing images

余生长醉 提交于 2019-12-06 02:11:27
Sampson

PHP...

You can re-size images with the gdlibrary (See: PHP/GD - Cropping and Resizing Images), but that may be complicated if you're not already pretty familiar with PHP.

jQuery/Javascript

A plugin exists that can dynamically resize images (stretching the image itself). Check out maxSide: http://css-tricks.com/maxside-jquery-plugin-and-how-to/

CSS...

A quick-solution for keeping the signature imaged tamed is to restrict their overflow with css:

<img src="randomimage.png" />

Becomes

<div class="sigBox"><img src="randomimage.png" /></div>

With the following CSS:

div.sigBox { overflow:hidden; width:50px; height:50px; }

This will hide large images, rather than allowing them to distort your content.

You would probably first want to take the images dimensions. Then you can maintain an aspect ratio, while setting a new size via the simple HTML height and width attributes for the img tag.

You may also want to consider hosting signatures locally. When a user uploads an image, you can do all of the resizing then via GD Library.

<img src="/img/foo.png" height="100" width="100" />

Height and Width are in pixels. This is if you want to resize the image in HTML (which downloads the full image then "squishes" it around to the specified sizes).

If you want to programmatically alter the physical image file, check out the PHP GD functions: http://us.php.net/manual/en/ref.image.php

For a forum I administered, we placed the signature block in a div with overflow: hidden set in the CSS. That way, a user who put in a massive 600x300 signature image and a paragraph of text gained no benefit from doing so - only the 400x90 area specified showed up. Seemed to work well.

As JoshL and John T pointed out, you can use php's built in image handling support to dynamically alter images. In your situation, you could simply make a php script that will load the image, resize it to appropriate dimensions, serve it and save a cached copy of the resized image to avoid processing overhead on subsequent requests.

You would end up using something like this in your HTML code

<img src="img.php?file=foobar.jpg">

Using Javascript:

function changeSize(imageOrTextId, tableId)
{
    if (document.getElementById(imageOrTextId).width > document.getElementById(tableId).width)
    {
        document.getElementById(imageOrTextId).width = document.getElementById(tableId).width;
    }
}

Example HTML:

<body onload="changeSize('image1', 'table1')">
    <table id="table1" width="400" height="400">
        <img src="randomimage.png" id="image1" />
    </table>
</body>

EDIT: It looks like John T also posted this way of doing it... Sorry I didn't notice it before posting.

Using css

<img src="randomimage.png" style="max-width:100px;max-height:100px;" />

This works in all the browsers I have tested

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
             <html xmlns="http://www.w3.org/1999/xhtml">
              <head>
             <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        </head>

       <body>
        <?php
      $thumb_width = 100;
      $thumb_height = 100;
        $full = imagecreatefromjpeg('sample.jpg');
        $width = imagesx($full);
     $height = imagesy($full);

      if($width < $height) {
$divisor = $width / $thumb_width;
       } 
       else {
$divisor = $height / $thumb_height;
     }

    $new_width= ceil($width / $divisor);
  $new_height = ceil($height / $divisor);

  //get center point
  $thumbx = floor(($new_width - $thumb_width) / 2);
  $thumby = floor(($new_height - $thumb_height)/2);

 $new_image = imagecreatetruecolor($new_width, $new_height);
  $new_image_fixed = imagecreatetruecolor($thumb_width, $thumb_height);

   imagecopyresized($new_image, $full, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

   imagecopyresized($new_image_fixed, $new_image, 0, 0, $thumbx, $thumby, $thumb_width,                                      $thumb_height, $thumb_width, $thumb_height);

     imagejpeg($new_image, "new_sample.jpg", 100);
    imagejpeg($new_image_fixed, "new_sample_fixed.jpg", 100);
     ?>
     </body>
      </html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!