加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Blueimp Jquery文件上传S3&Resize

发布时间:2020-12-13 17:21:48 所属栏目:PHP教程 来源:网络整理
导读:我正在使用 Blueimp/jQuery-File-Uploader和可用的Amazon S3插件,所有这些都很好但是我需要调整我的图像大小不要超过或低于640px的最短边. 我目前的代码是 global $s3; if (isset($_REQUEST['_method']) $_REQUEST['_method'] === 'DELETE') { return ""; }
我正在使用 Blueimp/jQuery-File-Uploader和可用的Amazon S3插件,所有这些都很好但是我需要调整我的图像大小不要超过或低于640px的最短边.

我目前的代码是

global $s3;
    if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
        return "";
    }
    $upload = isset($_FILES['files']) ? $_FILES['files'] : null;
    $info = array();
    if ($upload && is_array($upload['tmp_name'])) {
        foreach($upload['tmp_name'] as $index => $value) {
            $fileTempName = $upload['tmp_name'][$index];

            $file_name = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index]);
            $extension=end(explode(".",$file_name));
            $rand = rand(1,100000000);
            $sha1 = sha1($rand);
            $md5 = md5($sha1);
            $filename = substr($md5,8);
            $fileName=$filename.".".$extension;    

            $fileName = $prefix.str_replace(" ","_",$fileName);
            $response = $s3->create_object($bucket,$fileName,array('fileUpload' => $fileTempName,'acl' => AmazonS3::ACL_PUBLIC,'meta' => array('keywords' => 'example,test'),));
            if ($response->isOK()) {
                $info[] = getFileInfo($bucket,$fileName);
            } else {
                //     echo "<strong>Something went wrong while uploading your file... sorry.</strong>";

            }
        }

我已经写了这篇PHP,但我不确定如何让两者一起工作.

$image = new Imagick('test2.jpg');
$imageprops = $image->getImageGeometry();

$w=$imageprops['width'];
$h=$imageprops['height'];
$edge = min($w,$h);
$ratio = $edge / 640;

$tWidth = ceil($w / $ratio);
$tHeight = ceil($h / $ratio);

if ($imageprops['width'] <= 640 && $imageprops['height'] <= 640) {
    // don't upscale
} else {
    $image->resizeImage($tWidth,$tHeight,imagick::FILTER_LANCZOS,0.9,true);
}
$image->writeImage("test2-resized.jpg");

任何帮助都会感激不尽,谢谢

解决方法

这是基于OP消息中的所有代码都是正确的假设,并且只是根据请求重新安排它.

更新:四个upvotes(到目前为止)似乎表明OP不仅对代码是正确的,而且对问题的严重程度也是正确的.我做OSS是理所当然的,所以无论如何,请明确告诉我这是否对您有任何兴趣所以我们可以在github上改进这一点(任何行动都很好 – 提出问题,提出答案,发布评论或其任何组合).

function resize($imgName,$srcName)
{
    $image = new Imagick($imgName);
    $imageprops = $image->getImageGeometry();

    $w=$imageprops['width'];
    $h=$imageprops['height'];
    $edge = min($w,$h);
    $ratio = $edge / 640;

    $tWidth = ceil($w / $ratio);
    $tHeight = ceil($h / $ratio);

    if ($imageprops['width'] <= 640 && $imageprops['height'] <= 640) {
        return $imgName;
    } else {
        $image->resizeImage($tWidth,true);
    }
    $extension=end(explode(".",$srcName));
    // Change "/tmp" if you're running this on Windows
    $tmpName=tempnam("/tmp","resizer_").".".$extension;
    $image->writeImage($tmpName);
    return $tmpName
}

global $s3;
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
    return "";
}
$upload = isset($_FILES['files']) ? $_FILES['files'] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
    foreach($upload['tmp_name'] as $index => $value) {
        $file_name = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index]);
        $fileTempName = resize($upload['tmp_name'][$index],$file_name);
        $extension=end(explode(".",$file_name));
        $rand = rand(1,100000000);
        $sha1 = sha1($rand);
        $md5 = md5($sha1);
        $filename = substr($md5,8);
        $fileName=$filename.".".$extension;    

        $fileName = $prefix.str_replace(" ",$fileName);
        $response = $s3->create_object($bucket,));
        if ($response->isOK()) {
            $info[] = getFileInfo($bucket,$fileName);
        } else {
            //     `echo "<strong>Something went wrong while uploading your file... sorry.</strong>";`

        }
        unlink($fileTempName);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读