首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >imagesx()要求参数1为资源,给定字符串

imagesx()要求参数1为资源,给定字符串
EN

Stack Overflow用户
提问于 2020-11-01 12:36:14
回答 3查看 338关注 0票数 0

我之前尝试将多个文件上传到我的存储桶中,它工作得很好,但现在我尝试在上传之前调整图像大小和压缩图像,它抛出错误imagesx() expects parameter 1 to be resource, string given以下是代码:

代码语言:javascript
复制
foreach($_FILES as  $ind => $filegroup){

$count=0;
foreach($_FILES[$ind]['name']as  $actfile){

                
                $tmp=$_FILES[$ind]['tmp_name'][$count];


                $count=$count + 1;
$newind= str_replace('_',' ',$ind);
                $filenamenew= $insertedids[0].$newind."_".$count.".jpg";
                $temp='menu_images/'.$filenamenew; 



$im = imagecreatetruecolor(700, 700);
$bg = imagecolorallocate ( $im, 255, 255, 255 );
imagefilledrectangle($im,0,0,700,700,$bg);


$max_width=700;
 $max_height= 700;

 $width = imagesx($tmp);

    $height = imagesy($tmp);

    # taller
    if ($height > $max_height) {
        $width = ($max_height / $height) * $width;
        $height = $max_height;
    }

    # wider
    if ($width > $max_width) {
        $height = ($max_width / $width) * $height;
        $width = $max_width;
    }


$im2 = imagescale($tmp, $width, $height);

imagecopy($im, $im2, (imagesx($im)/2)-(imagesx($im2)/2), (imagesy($im)/2)-(imagesy($im2)/2), 0, 0, imagesx($im2), imagesy($im2));


imagejpeg($im, NULL , 70);

我正在尝试将这个最终的$im上传到存储桶中,我已经在我的cent操作系统中安装了gd库

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-11-02 14:23:51

关键问题是将sourcefile发布到s3存储桶我将其更改为body,并且我更改了我的php设置以允许文件上传到5MB

以下是解决方案:

代码语言:javascript
复制
$im = imagecreatetruecolor(700, 700);
$bg = imagecolorallocate ( $im, 255, 255, 255 );
imagefilledrectangle($im,0,0,700,700,$bg);



list( $width, $height , $image_type ) = getimagesize($tmp);

$max_width=700;
 $max_height= 700;

 

    # taller
    if ($height > $max_height) {
        $width = ($max_height / $height) * $width;
        $height = $max_height;
    }

    # wider
    if ($width > $max_width) {
        $height = ($max_width / $width) * $height;
        $width = $max_width;
    }


switch ($image_type)
    {
        case 1: $im3 = imagecreatefromgif($tmp); break;
        case 2: $im3 = imagecreatefromjpeg($tmp);  break;
        case 3: $im3 = imagecreatefrompng($tmp); break;
        default: return '';  break;
    }

$im2 = imagescale($im3, $width, $height);


imagecopy($im, $im2, (imagesx($im)/2)-(imagesx($im2)/2), (imagesy($im)/2)-(imagesy($im2)/2), 0, 0, imagesx($im2), imagesy($im2));



$imgdata = image_data($im);
function image_data($gdimage)
{
    ob_start();
    imagejpeg($gdimage,NULL,70);
    return(ob_get_clean());
}

并且在上传到存储桶时

代码语言:javascript
复制
$result = $s3->putObject([
    'Bucket' => 'yourbucket',
    'Key'    => 'filename',
    'Body' => $imgdata 
]);
票数 0
EN

Stack Overflow用户

发布于 2020-11-01 14:11:33

imagesx()需要一个图像资源作为参数,该参数由一个图像创建函数返回,例如imagecreatetruecolor()。如果您需要图像的高度或宽度,您可以使用getimagesize(),如下所示。

代码语言:javascript
复制
list( $width, $height ) = getimagesize( 'filepath' );
票数 1
EN

Stack Overflow用户

发布于 2020-11-01 15:02:12

试试这个:

代码语言:javascript
复制
...
foreach($_FILES[$ind]['name'] as $fid => $actfile){ // add $fid in here!
   ...
   list($width, $height) = getimagesize($tmp=$_FILES[$ind]['tmp_name'][$fid]);
   echo "width: $width; height: $height<br />\n";
   ...
}
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64628699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档