Skip to content

Instantly share code, notes, and snippets.

@alanyoshida
Created August 1, 2015 16:03
Show Gist options
  • Save alanyoshida/07159fc750d382473cca to your computer and use it in GitHub Desktop.
Save alanyoshida/07159fc750d382473cca to your computer and use it in GitHub Desktop.
USANDO JCROP E PHP PRA FAZER RECORTE EM IMAGEM EM BLOB
<?php
// if( empty($_POST)
// || !isset($_POST['id']) || empty($_POST['id'])
// || !isset($_POST['croppedImage']) || empty($_POST['croppedImage']))
// {
// echo "ERRO: Dados faltando.";
// exit;
// }
try{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
var_dump($_POST);
// exit;
$x = $_POST['x'];
$y = $_POST['y'];
$w = $_POST['w'];
$h = $_POST['h'];
$mimetype = $_POST['mime'];
if(!isset($x) || empty($x)){
throw new Exception("Parametro X não enviado", 1);
}
if(!isset($y) || empty($y)){
throw new Exception("Parametro Y não enviado", 1);
}
if(!isset($w) || empty($w)){
throw new Exception("Parametro W não enviado", 1);
}
if(!isset($h) || empty($h)){
throw new Exception("Parametro H não enviado", 1);
}
if(!isset($mimetype) || empty($mimetype)){
throw new Exception("Mimetype não identificado", 1);
}
$targ_w = $targ_h = 150;
$image_quality = 90;
$blob = file_get_contents('http://projetounimed.local/'.$_POST['blob']);
if(!isset($blob) || empty($blob)){
throw new Exception("Não foi possivel recuperar o blob.", 1);
}
// var_dump($blob);
// exit;
//switches content-type and calls the imagecreatefrom... function
// switch ($mimetype) {
// case 'image/gif':
// header ('Content-Type: image/gif');
// // $img_r = imagecreatefromgif($blob);
// break;
// case 'image/jpeg':
// header ('Content-Type: image/jpeg');
// // $img_r = imagecreatefromjpeg($blob);
// break;
// case 'image/png':
// header ('Content-Type: image/png');
// // $img_r = imagecreatefrompng($blob);
// break;
// case 'image/x-ms-bmp':
// header ('Content-Type: image/x-ms-bmp');
// // $img_r = imagecreatefromwbmp($blob);
// break;
// }
// $blob = base64_decode($blob);
// var_dump($blob);
// $blob = substr($blob, 22);
$img_r = imagecreatefromstring($blob);
if(!$img_r){
throw new Exception("Erro: imagecreatefromstring() Data is not in a recognized format.", 1);
}
// $img_r = imagecreatefromjpeg($src);
$dst_r = imagecreatetruecolor( $targ_w, $targ_h );
imagecopyresampled( $dst_r, $img_r, 0, 0, $x, $y, $targ_w, $targ_h, $w, $h);
ob_start();
// imagejpeg($resized);
// header('Content-type: '.$mimetype);
switch ($mimetype) {
case 'image/gif':
imagegif($dst_r, null, $image_quality);
break;
case 'image/jpeg':
imagejpeg($dst_r, null, $image_quality);
break;
case 'image/png':
imagepng($dst_r, null, 8);
break;
case 'image/x-ms-bmp':
imagewbmp($dst_r, null, $image_quality);
break;
}
$new_blob = ob_get_clean();
echo $new_blob;
// imagejpeg($dst_r,null,$jpeg_quality);
exit;
// $id = (int) $_POST['id'];
// $blob = $_POST['croppedImage'];
// $con = new PDO("mysql:host=localhost;dbname=unimedsymfony", "root", "");
// $sth = $con->prepare('UPDATE midias SET dados=:dados WHERE id=:id');
// $sth->bindValue(':id', $id, PDO::PARAM_INT);
// $sth->bindValue(':dados', $blob, PDO::PARAM_STR);
// $sth->execute();
}else{
throw new Exception("Formulário não enviado", 1);
}
}catch(Exception $e){
echo $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment