PHP:图像未保存在指定路径

PHP:图像未保存在指定路径,php,Php,我使用以下php脚本处理图片上传: // receive image data $imgUrl = $_POST['imgUrl']; $imgInitW = $_POST['imgInitW']; $imgInitH = $_POST['imgInitH']; $imgW = $_POST['imgW']; $imgH = $_POST['imgH']; $imgY1 = $_POST['imgY1']; $imgX1 = $_POST['imgX1']; $cropW = $_POST['c

我使用以下php脚本处理图片上传:

// receive image data
$imgUrl = $_POST['imgUrl'];
$imgInitW = $_POST['imgInitW'];
$imgInitH = $_POST['imgInitH'];
$imgW = $_POST['imgW'];
$imgH = $_POST['imgH'];
$imgY1 = $_POST['imgY1'];
$imgX1 = $_POST['imgX1'];
$cropW = $_POST['cropW'];
$cropH = $_POST['cropH'];

// set image quality
$jpeg_quality = 100;

// define output name
$output_filename = "uploads/users/croppedImg_".rand();

// make sure only specific types of images get uploaded
$what = getimagesize($imgUrl);
// get extension of image url
$imgUrlParts = explode(".", $imgUrl);
$imgExtension = end($imgUrlParts);
switch(strtolower($imgExtension)) {
    case 'png':
        $img_r = imagecreatefrompng($imgUrl);
        $source_image = imagecreatefrompng($imgUrl);
        $type = '.png';
        break;
    case 'jpeg':
    case 'jpg':
        $img_r = imagecreatefromjpeg($imgUrl);
        $source_image = imagecreatefromjpeg($imgUrl);
        $type = '.jpeg';
        break;
    case 'gif':
        $img_r = imagecreatefromgif($imgUrl);
        $source_image = imagecreatefromgif($imgUrl);
        $type = '.gif';
        break;
    default: die('image type not supported');
}

$resizedImage = imagecreatetruecolor($imgW, $imgH);
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);   

$dest_image = imagecreatetruecolor($cropW, $cropH);
imagecopyresampled($dest_image, $resizedImage, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);   

imagejpeg($dest_image, $output_filename.$type, $jpeg_quality);

$response = array(
    "status" => 'success',
    "url" => $output_filename.$type 
);

print json_encode($response);

我的问题是图片从未到达$output\u filename中指定的目录。有人知道为什么吗?可能有人知道需要在此处使用的其他代码。

首先,确保已打开错误报告,以查看是否有错误:

// Add these to the top of your php script if you can't turn it on in php.ini
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
然后我会检查图像是否正确创建。为此,您可以将
imagejpeg
中的filename参数替换为
NULL
,以便将图像直接输出到浏览器。如果它正常工作,那么可能是权限问题。将输出位置指定为绝对路径,并使用固定文件名进行调试


如果仍然不起作用,请发布出现的任何错误。

而不是创建代码。请从头开始使用可用的插件

其中一个易于使用的插件是EasyHPThumbnail,下面是一个链接

文件夹是否存在?Web服务器(apache?)是否具有写权限?