duan5731 2015-08-17 09:44
浏览 208

如何压缩图像的宽度和高度,而不是在500 x 500中裁剪

I am able to crop the image as 500 x 500 px after uploading using php. But, the problem is the image is getting cropped if it is compressed. I want the full image to be compressed or resized in 500 x 500 without cropping. Please help. I am providing my code for reference.

function resize_image($file, $width, $height) {
    list($w, $h) = getimagesize($file);

    /* calculate new image size with ratio */
    $ratio = max($width/$w, $height/$h);
    $h = ceil($height / $ratio);
    $x = ($w - $width / $ratio) / 2;
    $w = ceil($width / $ratio);

    /* read binary data from image file */
    $imgString = file_get_contents($file);

    /* create image from string */
    $image = imagecreatefromstring($imgString);
    $tmp = imagecreatetruecolor($width, $height);
    imagecopyresampled($tmp, $image,
        0, 0,
        $x, 0,
        $width, $height,
        $w, $h);
    $ext = pathinfo($file, PATHINFO_EXTENSION); 

    //$ext will be gif

    imagejpeg($tmp, $file, 100);
    return $file;

    /* cleanup memory */

    imagedestroy($image);
    imagedestroy($tmp);
}
  • 写回答

1条回答 默认 最新

  • drh37116 2015-08-17 10:41
    关注

    I think, that HTML img in echo will help:

    <?php
    echo'<img src="url of image" width="your img width" height="your img height">'
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号