dongnaosuan5407 2016-01-02 17:38
浏览 302

如何将Base64图像保存到images文件夹上的图像文件

I have a JQUERY image cropping plugin that stores a cropped result as Base64/JPEG. I'd like to save that Base64 IMG as an actual JPG in a folder called IMAGES in the root.

The example screenshot below shows you the Base64 image that I would like saved as file. The SAVE button doesn't do anything right now, but should perform my desired function.

I assume there will be some PHP as well that does the saving to IMAGES part. It would also be good if it saves the image file with some random name (IE - not image.jpg, but image_random52.jpg)

<input id="save_to_file" type="button" value="Save" onClick="MaybeSomethingShouldGoHERE???()"></div>

enter image description here

Also, here is the relevant PHP code that generated that Base64 Image.

$resizedImage = imagecreatetruecolor($imgW, $imgH);
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW,
            $imgH, $imgInitW, $imgInitH);
$dest_image = imagecreatetruecolor($cropW, $cropH);
imagecopyresampled($dest_image, $resizedImage, 0, 0, $imgX1, $imgY1, $cropW,
            $cropH, $cropW, $cropH);
ob_start();
imagejpeg($dest_image, null, $jpeg_quality);
$imgData = ob_get_clean();
ob_end_clean();
$response = array(
    "status" => 'success',
    "url" => 'data:'.$what['mime'].';base64,'.base64_encode($imgData),
  );

 echo json_encode($response);

UPDATED PHP CODE

$response = array(
    "status" => 'success',
    "url" => 'data:'.$what['mime'].';base64,'.base64_encode($imgData),
    $final_output = 'data:'.$what['mime'].';base64,'.base64_encode($imgData)
  );


   $decoded=base64_decode($final_output);

file_put_contents('newImage.JPG',$decoded);
//leave it to you to randomize the filename.
  • 写回答

1条回答 默认 最新

  • dousi1906 2016-01-02 23:21
    关注

    OK, Here was my solution. I didn't actually use OnClick of the Save button, but performed the save directly from my cropping PHP file. I added the last 3 lines staring from $final_output. This allowed me to save file to folder.

    Before I didn't understand the logic, but I now do.

      "status" => 'success',
        "url" => 'data:'.$what['mime'].';base64,'.base64_encode($imgData),
    
    $final_output = base64_encode($imgData)
    
      );
    
    //Decode and Save Image 
    $decoded=base64_decode($final_output);
    file_put_contents($output_filename.$type,$decoded);
    //leave it to you to randomize the filename.
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题