复制镜像文件


Copying image file

我正在上传一个图像文件,复制它,调整它的大小,然后移动原始文件并调整它的大小!

我已经写了下面的函数,我知道有很多空间来整理代码等。

public function useImage($image, $photoid){
    $source = $image['tmp_name'];
    $target = "projectimages/";
    //prepare the largest image
    copy($source, $target);
    $targetname = $photoid."large.jpg";
    $file = $target.$targetname;
    list($width, $height) = getimagesize($file);
    $modwidth = 800;
    $diff = $width / $modwidth;
    $modheight = $height / $diff;
    $tn = imagecreatetruecolor($modwidth, $modheight);
    $image = imagecreatefromjpeg($file);
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
    imagejpeg($tn, $file, 100);
    //prepare the smaller image
    move_uploaded_file($source, $target);
    $targetname = $photoid."small.jpg";
    $file = $target.$targetname;
    list($width, $height) = getimagesize($file);
    $modwidth = 400;
    $diff = $width / $modwidth;
    $modheight = $height / $diff;
    $tn = imagecreatetruecolor($modwidth, $modheight);
    $image = imagecreatefromjpeg($file);
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
    imagejpeg($tn, $file, 100);
}

我得到了很多错误,但关键的一个,其他的是建立在第一个,当我试图复制或移动上传的文件…

Warning: copy(projectimages/) [function.copy]: failed to open stream: Is a directory in /Applications/MAMP/htdocs/bs/classes/image.php on line 171

我已经对图像使用了var_dump,看起来图像已经就位了。

任何想法?

copy()呼叫的目标是一个目录。试着这样修改你的代码:

$source = $image['tmp_name'];
$target = "projectimages/";
//prepare the largest image
$targetname = $photoid."large.jpg";
$file = $target.$targetname;
copy($source, $file);
// The rest of your code goes here.