PHP GD图像裁剪允许的内存大小已耗尽
我在我的网站上建立了一个图像裁剪系统,它通过用户上传获取图像,允许用户裁剪图像,然后通过PHP GD裁剪图像并将其上传到服务器 由于某种原因,当我有一个非常大的图像时,比如4000 x 6000(这是将要上传的大多数图像),我会得到一个PHP服务器错误 查看我的日志后,我发现了这个错误PHP GD图像裁剪允许的内存大小已耗尽,php,image,codeigniter,gd,crop,Php,Image,Codeigniter,Gd,Crop,我在我的网站上建立了一个图像裁剪系统,它通过用户上传获取图像,允许用户裁剪图像,然后通过PHP GD裁剪图像并将其上传到服务器 由于某种原因,当我有一个非常大的图像时,比如4000 x 6000(这是将要上传的大多数图像),我会得到一个PHP服务器错误 查看我的日志后,我发现了这个错误 PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 37105903 bytes) in U
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 37105903 bytes) in Unknown on line 0
注意:我尝试了ini_集('memory_limit','-1')代码>
下面是从HTML表单检索base 64编码图像的代码
$imagebased64 = $_POST['imagebase64']; // base 64 image
$center_id = $_POST['center_id']; // user id
$filename = $this->helper_model->resize_profile_image($imagebased64, $center_id); // resize the image and get the file name
$insert['image'] = $filename; // insert into database
这是调整图像大小的功能:
/**
* Returns an edit button - used to edit ticket row data
*
*
* @param base64 $data image to be cropped
* @param int $center_id hashed center_id to be decrypted
* @return string the file name of the image
*/
public function resize_profile_image($data, $center_id) {
ini_set('memory_limit', '-1');
$center_id = $this->helper_model->convert_hash_to_center_id($center_id);
// old dimensions
$org_w = getimagesize($data)[0];
$org_h = getimagesize($data)[1];
// new dimensions
$targ_w = 542;
$targ_h = 671;
$data = substr($data, 22);
$data = base64_decode($data);
$data = imagecreatefromstring($data);
$image_p = imagecreatetruecolor($targ_w, $targ_h);
imagecopyresampled($image_p, $data, 0, 0, 0, 0, $targ_w, $targ_h, $org_w, $org_h);
// Buffering
ob_start();
imagepng($image_p);
$data = ob_get_contents();
ob_end_clean();
//Store & Display
$context = stream_context_create([
'gs' =>[
'acl'=> 'public-read',
'Content-Type' => 'image/jpeg',
'enable_cache' => true,
'enable_optimistic_cache' => true,
'read_cache_expiry_seconds' => 300,
]
]);
$name = $this->user_model->get_full_name($center_id);
$filename = $name.'.jpg';
file_put_contents(APPPATH.'../assets/developed/profile_images/'.$filename, $data, false, $context);
return $filename;
}
您是在VPS还是共享服务器上?查看主机的内存限制。如果需要处理大型图像,则必须分配更多内存。如果没有资源,您无法执行作业。我正在使用MAMP在本地主机上运行。