PHP裁剪不工作,返回黑色图像-图像裁剪系统

PHP裁剪不工作,返回黑色图像-图像裁剪系统,php,Php,基本上,我正在开发一个图像裁剪系统。我正在尝试调整大小并将图像转换为jpeg格式,然后允许执行裁剪功能。但是,它的大小和转换没有问题。但是当我添加裁剪函数时,问题是它返回一个裁剪过的黑色图像。这方面的信息将通过jQueryAjax发布。我知道ajax帖子可以正常工作,因为我调试了它。我可以为您提供我的客户端脚本,尽管我认为这不是必需的,因为问题似乎出在PHP中 更新:我已经修改了我的脚本。这个问题仍然存在 $old_file_path = $user_data['profile_pic']; $

基本上,我正在开发一个图像裁剪系统。我正在尝试调整大小并将图像转换为jpeg格式,然后允许执行裁剪功能。但是,它的大小和转换没有问题。但是当我添加裁剪函数时,问题是它返回一个裁剪过的黑色图像。这方面的信息将通过jQueryAjax发布。我知道ajax帖子可以正常工作,因为我调试了它。我可以为您提供我的客户端脚本,尽管我认为这不是必需的,因为问题似乎出在PHP中

更新:我已经修改了我的脚本。这个问题仍然存在

$old_file_path = $user_data['profile_pic'];
$new_width = $_POST['width_img'];
$new_height = $_POST['height_img'];
$dst_x = 0;
$dst_y = 0;
$src_x = $_POST['left'];
$src_y = $_POST['top'];
$dst_w = $_POST['width'];
$dst_h = $_POST['height'];
$src_w = $_POST['width'];
$src_h = $_POST['height'];

    $file_path = 'core/images/profile/'.  substr(md5(time()), 0, 15) . '.' . 'jpg';

    $old_file_name = $user_data['profile_pic'];
    $allowed = array('jpg','jpeg','gif','png');
    $file_extension= explode('.', $old_file_name); 
    $file_extn= strtolower(end($file_extension));

    $image_p = imagecreatetruecolor($new_width, $new_height);

    if($file_extn == 'jpg' || $file_extn == 'jpeg'){
    $image = imagecreatefromjpeg($old_file_name);
}if ($file_extn == 'png'){
    $image = imagecreatefrompng($old_file_name);
}

list($width, $height) = getimagesize($old_file_name);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $file_path);
//Begin cropping!
$dst_image = imagecreatetruecolor($dst_w,$dst_h);
$src_image = imagecreatefromjpeg($old_file_name);

imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagejpeg($dst_image, $file_path);

crop_profile_image($session_user_id, $file_path, $old_file_path);

function crop_profile_image($user_id, $file_path, $old_file_path){
    if(file_exists($old_file_path) === true){unlink($old_file_path);}   
    mysql_query("UPDATE `users` SET `profile_pic` = '". mysql_real_escape_string($file_path) ."' WHERE `user_id` = " . (int)$user_id);
}

使用timthumb,它是一个单一的文件,非常容易使用,而且有很多选项。有关更多信息,请访问:


谢谢。

您可以找到
imagecreatetruecolor
并在其后面添加此
函数
imagecoloralocate
。 例如:


timthumb不再受支持,请查看您提供的链接以供将来参考;Timthumb使用起来非常不安全,因此不再是一个好的建议。
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); //find this function
imagecolorallocate($newImage, 0, 0, 0); //add this function
imagecolortransparent($newImage, $transpatent);