dtjkl42086 2014-11-20 00:17
浏览 59
已采纳

上传时复制的图像大小为缩略图

I am working on an uploader and slowly getting it working, I am uploading 3 images at once, and setting arrays for each one as keys, with an increment of ++1. I am wanting to resize the image before it gets copied to the thumbnail folder.

I have this code.

Everything works with it. As you see, I started on getting the file info, but after that I am totally stuck on what to do after to resize the image proportionally with a maximum width of xpx and height to match it without looking distorted.

Any help would be really appreciated. Thank You.

EDIT --- I started working on it myself and wondering if this is the right approach to what i am doing.

<?php
if (isset($_POST['addpart'])) {

    $image = $_FILES['images']['tmp_name'];
    $name  = $_POST['username'];
    $i     = 0;
    foreach ($image as $key) {

        $fileData   = pathinfo(basename($_FILES["images"]["name"][$i]));
        $fileName[] = $name . '_' . uniqid() . '.' . $fileData['extension'];
        move_uploaded_file($key, "image/" . end($fileName));
        copy("image/" . end($fileName), "image_thumbnail/" . end($fileName));  


         // START -- THE RESIZER THAT IS BEING WORKED ON 
        $source  = "image_thumb/" . end($fileName);
        $dest    = "image_thumb/" . end($fileName);
        $quality = 100;
        $scale   = 1 / 2;

        $imsize = getimagesize($source);
        $x      = $scale * $imsize[0];
        $y      = $scale * $imsize[1];

        $im    = imagecreatefromjpeg($source);
        $newim = imagecreatetruecolor($x, $y);
        imagecopyresampled($newim, $im, 0, 0, 0, 0, $x, $y, $imsize[0], $imsize[1]);
        imagejpeg($newim, $dest, $quality);
        // END -- THE RESIZER THAT IS BEING WORKED ON 
        $i++;
    }
    echo 'Uploaded<br>';
    echo 'Main Image - ' . $fileName[0] . '<br>';
    echo 'Extra Image 1 - ' . $fileName[1] . '<br>';
    echo 'Extra Image 2 - ' . $fileName[2] . '<br>';
    echo '<hr>';

}
?>

thanks

  • 写回答

2条回答 默认 最新

  • doumei4964 2014-11-20 00:27
    关注

    Use GD library.

    1. Create input image object using imagecreatefromstring() for example: imagecreatefromstring(file_get_contents($_FILES['images']['tmp_name'][$i]))

      It's the simplest way.

      Another option is to detect file type and use functions like imagecreatefromjpeg (), imagecreatefrompng(), etc.

    2. Create output empty image using imagecreate()
    3. Use imagecopyresampled() or imagecopyresized() to resize image and copy+paste it from input image to output image
    4. Save output image using function like imagejpeg()
    5. Clean memory using imagedestroy()
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了