sark9012
sark9012

Reputation: 5737

Copying image file

I'm uploading an image file, copying it, resizing it, then moving the original file and resizing it!

I've written the following function, I know there's a lot of room to tidy the code up etc.

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

}

I'm getting plenty of errors but the crucial one that the others are built upon is the first one when I try and copy or move the uploaded file...

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

I've used var_dump on image and it appears the image is in place.

Any ideas?

Upvotes: 0

Views: 54

Answers (1)

napolux
napolux

Reputation: 16074

The destination of the copy() call is a directory. Try to change your code like this:

$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.

Upvotes: 1

Related Questions