php – Blueimp Jquery文件上传S3&Resize

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Blueimp Jquery文件上传S3&Resize脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 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);
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – Blueimp Jquery文件上传S3&Resize全部内容,希望文章能够帮你解决php – Blueimp Jquery文件上传S3&Resize所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。