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

如何将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 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿