复制图像文件

时间:2014-03-20 16:07:58

标签: php

我正在上传图片文件,复制图片文件,调整图片大小,然后移动原始文件并调整其大小!

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

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,看起来图像就位了。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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.