Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rotating (max top and max left for angle 90) and overs mini fix #267

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed assets/.DS_Store
Binary file not shown.
Binary file removed assets/css/.DS_Store
Binary file not shown.
Binary file removed assets/img/.DS_Store
Binary file not shown.
Binary file removed assets/js/.DS_Store
Binary file not shown.
834 changes: 446 additions & 388 deletions croppic.js

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions croppic.min.js

This file was deleted.

Binary file removed croppic.zip
Binary file not shown.
Binary file removed croppicWeb.zip
Binary file not shown.
130 changes: 71 additions & 59 deletions img_crop_to_file.php
Original file line number Diff line number Diff line change
@@ -1,88 +1,100 @@
<?php
/*
* !!! THIS IS JUST AN EXAMPLE !!!, PLEASE USE ImageMagick or some other quality image processing libraries
*/
$imgUrl = $_POST['imgUrl'];
$tmp_dir = $_SERVER['DOCUMENT_ROOT'] . '/tmp/';
$filename = explode('/', $_POST['imgUrl']);
$filename = $filename[count($filename) - 1];
$imgUrl = $tmp_dir . $filename;

// original sizes
$imgInitW = $_POST['imgInitW'];
$imgInitH = $_POST['imgInitH'];

// resized sizes
$imgW = $_POST['imgW'];
$imgH = $_POST['imgH'];

// offsets
$imgY1 = $_POST['imgY1'];
$imgX1 = $_POST['imgX1'];

// crop box
$cropW = $_POST['cropW'];
$cropH = $_POST['cropH'];
$cropW = $image_config[1]['w'];
$cropH = $image_config[1]['h'];

// rotation angle
$angle = $_POST['rotation'];

$jpeg_quality = 100;

$output_filename = "temp/croppedImg_".rand();

// uncomment line below to save the cropped image in the same location as the original image.
//$output_filename = dirname($imgUrl). "/croppedImg_".rand();

$output_filename = $client->client_id.time();
while (file_exists($tmp_dir . $output_filename)) {
$filename_array = explode('.', $output_filename);
$output_filename = $filename_array[0] . '_' . mt_rand(0, 1000) . '.' . $filename_array[count($filename_array) - 1];
}
$what = getimagesize($imgUrl);

switch(strtolower($what['mime']))
{
switch (strtolower($what['mime'])) {
case 'image/png':
$img_r = imagecreatefrompng($imgUrl);
$source_image = imagecreatefrompng($imgUrl);
$type = '.png';
break;
$img_r = imagecreatefrompng($imgUrl);
$source_image = imagecreatefrompng($imgUrl);
$type = '.png';
break;
case 'image/jpeg':
$img_r = imagecreatefromjpeg($imgUrl);
$source_image = imagecreatefromjpeg($imgUrl);
error_log("jpg");
$type = '.jpeg';
break;
$img_r = imagecreatefromjpeg($imgUrl);
$source_image = imagecreatefromjpeg($imgUrl);
error_log("jpg");
$type = '.jpeg';
break;
case 'image/gif':
$img_r = imagecreatefromgif($imgUrl);
$source_image = imagecreatefromgif($imgUrl);
$type = '.gif';
break;
default: die('image type not supported');
$img_r = imagecreatefromgif($imgUrl);
$source_image = imagecreatefromgif($imgUrl);
$type = '.gif';
break;
default:
$arr = Array(
"status" => 'error',
"message" => 'Тип файла не поддержвивается. Выберите другой файл'
);
break;
}

$output_filename .= $type;

//Check write Access to Directory

if(!is_writable(dirname($output_filename))){
$response = Array(
"status" => 'error',
"message" => 'Can`t write cropped File'
);
}else{

if (!is_writable(dirname($tmp_dir.$output_filename))) {
$arr = Array(
"status" => 'error',
"message" => 'Ошибка загрузки файла'
);
} else {
// resize the original image to size of editor
$resizedImage = imagecreatetruecolor($imgW, $imgH);
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
$image = imagecreatetruecolor($imgInitW, $imgInitH);
imageAlphaBlending($image, false);
imageSaveAlpha($image, true);

// rotate the rezized image
$rotated_image = imagerotate($resizedImage, -$angle, 0);
// find new width & height of rotated image
$rotated_width = imagesx($rotated_image);
$rotated_height = imagesy($rotated_image);
// diff between rotated & original sizes
$dx = $rotated_width - $imgW;
$dy = $rotated_height - $imgH;
// crop rotated image to fit into original rezized rectangle
$cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
// crop image into selected area
$final_image = imagecreatetruecolor($cropW, $cropH);
imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
// finally output png image
//imagepng($final_image, $output_filename.$type, $png_quality);
imagejpeg($final_image, $output_filename.$type, $jpeg_quality);
$response = Array(
if ($angle) {
$image = imagerotate($source_image, -$angle, 0);
} else {
$image = $source_image;
}
$image2 = imagecreatetruecolor($imgW, $imgH);
imageAlphaBlending($image2, false);
imageSaveAlpha($image2, true);
imagecopyresampled($image2, $image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);

// crop image into selected area
$final_image = imagecreatetruecolor($cropW, $cropH);
imageAlphaBlending($final_image, false);
imageSaveAlpha($final_image, true);
//imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
imagecopyresampled($final_image, $image2, 0, 0, $imgX1, $imgY1, $cropW+1, $cropH+1, $cropW, $cropH);

// finally output png image
//imagepng($final_image, $tmp_dir.$output_filename, $png_quality);
imagejpeg($final_image, $tmp_dir.$output_filename, $jpeg_quality);
$arr = Array(
"status" => 'success',
"url" => $output_filename.$type
);
"url" => '/tmp/'.$output_filename
);
}
print json_encode($response);
echo json_encode($arr);