我在我的网站上构建了一个图像裁剪系统,通过用户上传获取图像,允许用户裁剪,然后通过PHP GD裁剪并将其上传到服务器。
出于某种原因,当我有一个非常大的图像,例如4000 x 6000(这是将要上传的大部分图像)时,我收到了PHP服务器错误。
查看我的日志后,我看到了这个错误,
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 37105903 bytes) in Unknown on line 0
注意:我已尝试ini_set('memory_limit', '-1');
以下是从HTML表单中检索基本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;
}