I try to upload multiple files to my bucket earlier it worked well but now I try to resize and compress the image before upload, it throws error imagesx() expects parameter 1 to be resource, string given
here is the code :
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);
I am trying to upload this final $im
to bucket , I have already installed gd library in my cent os
Advertisement
Answer
The key problem was posting sourcefile
to s3 bucket I changed that to body
, also I changed my php settings to allow file upload till 5 mb
Here is the solution:
$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()); }
And while uploading to bucket
$result = $s3->putObject([ 'Bucket' => 'yourbucket', 'Key' => 'filename', 'Body' => $imgdata ]);