2011-03-28 101 views
0

我使用这段代码来制作上传图像的缩略图。PHP GD图像裁剪功能

$file = randString().'.jpg'; 
    $uniPath = 'i/u'.$login; 
    $completePath = $uniPath.'/a/'.$file; 
    $thumbPath = $uniPath.'/b/'.$file; 
    if(copy($_FILES['filename']['tmp_name'], $completePath)){ 

function convertPic($w_dst, $h_dst, $n_img){ 
    $wxh = $w_dst.'x'.$h_dst; 
    switch($wxh){ 
    case '150x150': $letter = 'b'; break; 
    case '50x50': $letter = 'c'; break; 
    default: $letter = 'z'; 
    } 
    $dbPath = '/i/u1/'.$letter.'/'.$n_img; 
    $new_img = $_SERVER['DOCUMENT_ROOT'].$dbPath; 
    $file_src = "img.jpg"; // temporary safe image storage 
    $img_src = $file_src; 
    unlink($file_src); 
    move_uploaded_file($_FILES['filename']['tmp_name'], $file_src); 

    list($w_src, $h_src, $type) = getimagesize($file_src);  // create new dimensions, keeping aspect ratio 

    $ratio = $w_src/$h_src; 

    $h_ratio = ($h_dst/$h_src); 
    $w_ratio = ($w_dst/$w_src); 

    if($w_src > $h_src){ //landscape 
    $w_crop = round($w_src * $h_ratio); 
    $h_crop = $h_dst; 
    $src_x = ceil(($w_src - $h_src)/2); 
    $src_y = 0; 
    } 
    elseif($w_src < $h_src){ // portrait 
    $h_crop = round($h_src * $w_ratio); 
    $w_crop = $w_dst; 
    $src_y = ceil(($h_src - $w_src)/2); 
    $src_x = 0; 
    } 
    else { //square 
    $w_crop = $w_dst; 
    $h_crop = $h_dst; 
    $src_x = 0; 
    $src_y = 0; 
    } 

    switch ($type) 
    {case 1: // gif -> jpg 
     $img_src = imagecreatefromgif($file_src); 
     break; 
     case 2: // jpeg -> jpg 
     $img_src = imagecreatefromjpeg($file_src); 
     break; 
     case 3: // png -> jpg 
     $img_src = imagecreatefrompng($file_src); 
     break; 
    } 
    $img_dst = imagecreatetruecolor($w_dst, $h_dst); // resample 
    imagecolorallocate($img_dst, 255, 255, 255) or die("fail imagecolorallocate"); 

    imagecopyresampled($img_dst, $img_src, 0, 0, $src_x, $src_y, $w_crop, $h_crop, $w_src, $h_src) or die("imagecopyresampled($img_dst, $img_src, 0, 0, $src_x, $src_y, $w_crop, $h_crop, $w_src, $h_src)"); 
    imagejpeg($img_dst, $new_img); // save new image 

    unlink($file_src); // clean up image storage 
    imagedestroy($img_src);  
    imagedestroy($img_dst); 
    return $db_path; 
    } 

convertPic(150, 150, $file); 
convertPic(250, 250, $file); 

但出于某种原因,如果调用两次在上面的例子中,convertPic功能不起作用。如果它被称为一旦一切正常。我已经把一个警告,如果imagecopyresampled失败,它输出

imagecopyresampled(资源ID#17, img.jpg,0,0,0,0,250,250,,)

我认为问题是临时图像存储但不确定。请帮忙。

回答

0

我猜问题是取消链接($ file_src)...

首先,你叫convertPic()功能,因为你的img.jpg还在这里它工作正常,那么你unlink()删除您的图片,并尝试再次调用实际需要此文件才能正常工作的相同例程。

此外,您不能将同一个文件移动两次,因此您必须将其移出该功能,根据需要多次执行任务,然后取消链接图像。取消链接后,应该在convertPic()move_uploaded_file()双重呼叫之后应该在功能convertPic()之前。

那么,这是我一见钟情。

1

这似乎是你正在处理上传的图像。作为处理功能的一部分,您将上传的文件从其临时目录中移出,然后对其进行一些处理。

当您第二次再次调用该函数时,上传的文件不再存在于临时目录中,事情就会崩溃。

function convertPic(...) { 
    .... 
    move_uploaded_file($_FILES['filename']['tmp_name'], $file_src); 
    .... 
    unlink($file_src); 
    .... 
} 

基本上以convertPic进程,然后在第一次调用删除“源”,这意味着它不再可用于第二呼叫之后立即。