Resizing images

2019-05-17 08:49发布

I'm working on a forum application in PHP. In signature field I have
"<img src='randomimage.png'><br>bla blah"

If an image is bigger than the field it stretches my tables and looks bad. Is there any way to re size the image if its too big?

Sorry for my poor English and thank you for reading

Edit: The thing is that it's not only the image. It's the image and the text "the big text".

Respectfully, Tom

8条回答
疯言疯语
2楼-- · 2019-05-17 09:36
     <!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>
查看更多
甜甜的少女心
3楼-- · 2019-05-17 09:40
<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

查看更多
登录 后发表回答