Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

matheusmarson

Salvar imagem gerado com o Cropit

Recommended Posts

Olá a todos

Alguém saberia me dizer como faço para salvar a image gerado pelo cropit em uma pasta
Ao invés dela ser exibida em uma nova aba gostaria de salvá-la

Tentei o seguinte:
Código html:

<body>

    <div class="image-editor">
      <input type="file" class="cropit-image-input">
      <div class="cropit-image-preview"></div>
      <div class="image-size-label">
        Resize image
      </div>
      <input type="range" class="cropit-image-zoom-input">
      <input type="hidden" name="image-data" class="hidden-image-data" />
      <button class="export">Export</button>
    </div>

<script>
$(function() {
        $('.image-editor').cropit({
		imageState: {
			src: 'fotos/sem_foto_capa.jpg'
		}
        });

        $('.export').click(function() {
		var imageData = $('.image-editor').cropit('export');
		$('.hidden-image-data').val(imageData);
		
		if (imageData){
			$.ajax({
			type: "POST",
			url: "funcoes/cropit.php",
			cache: false,
			data: "imagem=" + imageData,
			beforeSend: function(){
				//enviando
			},
			success: function(foto){
				alert(foto);
				window.open(foto);
			}
		});
		}
        });
      });
</script>
</body>

E o arquivo php que deveria salvar a imagem:

<?php
	session_start();
	$foto = $_POST["imagem"];
	$_SESSION["foto_capa"] = Date("YmdHis") . ".png";
	
	$destino = '../fotos/' . $_SESSION["foto_capa"];

	//decode the url, because we want to use normal charackters to use explode
	$decoded = urldecode($foto);
	//explode at ',' - the last part should be the encoded image now
	$exp = explode(',', $decoded);
	//we just get the last element with array_pop
	$base64 = array_pop($exp);
	//decode the image and finally save it
	$data = base64_decode($base64);
	//make sure you are the owner and have the rights to write content
	file_put_contents($destino, $data);

	
?>

O arquivo da imagem até é gerado na pasta mas não abre dá err como se a imagem não fosse uma imagem mesmo.

 

Alguém saberia como resolver isso?

 

obrigado

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

js

...

$('.export').click(function(){
  var data = $('.image-editor').cropit('export');
  $.post('salva.php', {img:data}).done(function(data){
     alert(data);
  });
});

...

salva.php

if (isset($_POST['img'])){
if (!preg_match_all('/^data:image\/(.*);base64,(.*)$/m', $_POST['img'], $match)) 
die ('ERRO AO OBTER IMAGEM');
$img_name = md5(uniqid(rand(), true)).'.'.$match[1][0];
$img_source = base64_decode($match[2][0]);
if (file_put_contents($img_name, $img_source) === FALSE) die ('ERRO AO SALVAR IMAGEM');
print "ARQUIVO SALVO COM SUCESSO\n\nNOME: {$img_name}";
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.