我已经在用PHP编写的名为Sngine的社交网络CMS中添加了裁剪功能。 我添加了一个jquery插件,以便用户可以选择他们想要的图像区域。 我使用imagecopyresampled来选择所需的图像坐标,但是出现问题并且它没有按预期工作,很多图像没有被复制,最终输出仍然保留黑色背景。
这是最终输出
我真的不确定我在这里做错了什么,许多教程使用相同的算法来完成裁剪。坐标正在从crop插件正确传递到php脚本。
以下是我的代码
<?php
/**
* ajax -> data -> report
*
* @package Sngine
* @author Zamblek
*/
// fetch bootstrap
require('../../../bootstrap.php');
// fetch image class
require('../../class-image.php');
// check AJAX Request
is_ajax();
// user access
user_access(true);
try {
$x = $_POST['crop']['x'];
$y = $_POST['crop']['y'];
$x2 = $_POST['crop']['x2'];
$y2 = $_POST['crop']['y2'];
$width = $_POST['crop']['w'];
$height = $_POST['crop']['h'];
$file_path = $_POST['crop_file'];
$file_path = $system['uploads_directory'].'/'.$file_path;
$file_path = '../../../'.$file_path;
$image = new Image($file_path);
$type = $image->get_type();
//crop and resize image
$newImage = imagecreatetruecolor($width,$height);
switch($type) {
case "image/gif":
$source = imagecreatefromgif($file_path);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$source = imagecreatefromjpeg($file_path);
break;
case "image/png":
case "image/x-png":
$source = imagecreatefrompng($file_path);
break;
}
$src_w = imagesx($source);
$src_h = imagesy($source);
//imagecopy($newImage,$source,0,0,$x,$y,$width,$height);
imagecopyresampled($newImage,$source,0,0,$x,$y,$width,$height,$src_w,$src_h);
switch($type) {
case "image/gif":
imagegif($newImage,$file_path);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
imagejpeg($newImage,$file_path,90);
break;
case "image/png":
case "image/x-png":
imagepng($newImage,$file_path);
break;
}
imagedestroy($newImage);
return_json(['file' => $_POST['crop_file']]);
} catch (Exception $e){
modal(ERROR, __("Error"), $e->getMessage());
}