2014-09-12 24 views
0

我用下面的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['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_filename指定的目录。有谁知道为什么?也许有人知道需要在这里使用一些额外的代码。

+0

是否文件夹中? webserver(apache?)是否具有写入权限? – ggutenberg 2014-09-12 15:09:54

回答

0

,首先要确保你有错误报告打开,看看是否有周围抛出一些错误:

// 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); 

然后我会检查是否被正确创建的映像。要做到这一点,你可以用NULL替换imagejpeg中的文件名参数,以将图像直接输出到浏览器。如果它正在工作,那么它可能是一个权限问题。将输出位置作为绝对路径,并用于调试使用固定的文件名。

如果它仍然无法正常工作,请张贴出现的任何错误。