I am wondering why this script doesn't work. With jQuery's $.post
I execute a PHP script, in which an image gets uploaded. After that, the image gets cropped and saved in a new directory. Locally this script works fine, but online I get a 500 Error. The image (or images, since the jquery is in a loop) get uploaded fine, but the cropping part doesn't proceed. I get only empty directories. Any idea why this happens?
PHP:
session_start();
$id = session_id();
$imageData = $_POST["imageData"];
$i=1;
while(file_exists("../".$id."/data".$i.".png")) {
$i++;
}
if ($_POST["imageData"]){
$decoded = $imageData;
$exp = explode(',', $decoded);
$base64 = array_pop($exp);
$data = base64_decode($base64);
$file = "data".$i.".png";
$success=file_put_contents('../'.$id.'/'.$file, $data);
if($success) {
mkdir("../" . $id . "/data" . $i, 0775);
$imagesize = getimagesize('data://application/octet-stream;base64,' . base64_encode($data));
$width = $imagesize[0];
$height = $imagesize[1];
$im = imagecreatefromstring($data);
$im1 = imagecrop($im, ['x' => 0, 'y' => 0, 'width' => $width / 2, 'height' => $height / 2]);
$im2 = imagecrop($im, ['x' => $width / 2, 'y' => 0, 'width' => $width / 2, 'height' => $height / 2]);
$im3 = imagecrop($im, ['x' => $width / 2, 'y' => $height / 2, 'width' => $width / 2, 'height' => $height / 2]);
$im4 = imagecrop($im, ['x' => 0, 'y' => $height / 2, 'width' => $width / 2, 'height' => $height / 2]);
if ($i == 5) {
$im1 = imagerotate($im1, 90, 1);
$im2 = imagerotate($im2, -90, 1);
$im3 = imagerotate($im3, -90, 1);
$im4 = imagerotate($im4, 90, 1);
} elseif ($i == 6) {
$im1 = imagerotate($im1, -90, 1);
$im2 = imagerotate($im2, 90, 1);
$im3 = imagerotate($im3, 90, 1);
$im4 = imagerotate($im4, -90, 1);
} elseif ($i == 7) {
$im1 = imagerotate($im1, -90, 1);
$im2 = imagerotate($im2, 90, 1);
$im3 = imagerotate($im3, -90, 1);
$im4 = imagerotate($im4, 90, 1);
} elseif ($i == 9) {
$im1 = imagerotate($im1, -90, 1);
$im2 = imagerotate($im2, 90, 1);
$im3 = imagerotate($im3, -90, 1);
$im4 = imagerotate($im4, 90, 1);
} elseif ($i == 11) {
$im3 = imagerotate($im3, 180, 1);
$im4 = imagerotate($im4, 180, 1);
} elseif ($i == 12) {
$im1 = imagerotate($im1, 180, 1);
$im2 = imagerotate($im2, 180, 1);
}
imagepng($im1, "../" . $id . "/data" . $i . "/a" . $i . ".png");
imagepng($im2, "../" . $id . "/data" . $i . "/b" . $i . ".png");
imagepng($im3, "../" . $id . "/data" . $i . "/c" . $i . ".png");
imagepng($im4, "../" . $id . "/data" . $i . "/d" . $i . ".png");
}
JQuery:
function exportImg(val){
var i = 0;
$.post('php/createdir.php');
for(val;val<=12;val++){
var imagenr = document.getElementById('preview'+val).children[0].id.replace ( /[^\d.]/g, '' );
var imageData = $('#image-cropper'+imagenr).cropit('export', {originalSize: true});
$.post('php/upload.php', { imageData: imageData }, function(){i++; if (i==12){window.location = 'success.php'}});
}
}
EDIT:
I checked the error logs. It states: Call to undefined function imagecrop() on line 24 But why is that so?