mercredi 15 avril 2015

saving cropped image " failed to open stream: Permission denied"

I've got a problem with my PHP. I'm trying to edit an avatar image, by cropping and/or rotating it. The source image can be uploaded via input type=file or you can choose an image already in the server. When I try to edit an image uploaded the code works great and my image are cropped and rotated, but when I try to edit an image picked from the server I get a Warning: imagepng(img/avatar): failed to open stream: Permission denied error, all my files are on 777.


Here is my code:



<?php
private function crop($src, $dst, $data) {
if (!empty($src) && !empty($dst) && !empty($data)) {
switch ($this -> type) {
case IMAGETYPE_GIF:
$src_img = imagecreatefromgif($src);
break;

case IMAGETYPE_JPEG:
$src_img = imagecreatefromjpeg($src);
break;

case IMAGETYPE_PNG:
$src_img = imagecreatefrompng($src);
break;
}

if (!$src_img) {
$this -> msg = "Failed to read the image file";
return;
}

$size = getimagesize($src);
$size_w = $size[0]; // natural width
$size_h = $size[1]; // natural height

$src_img_w = $size_w;
$src_img_h = $size_h;

$degrees = $data -> rotate;

// Rotate the source image
if (is_numeric($degrees) && $degrees != 0) {
// PHP's degrees is opposite to CSS's degrees
$new_img = imagerotate( $src_img, -$degrees, imagecolorallocatealpha($src_img, 0, 0, 0, 127) );

imagedestroy($src_img);
$src_img = $new_img;

$deg = abs($degrees) % 180;
$arc = ($deg > 90 ? (180 - $deg) : $deg) * M_PI / 180;

$src_img_w = $size_w * cos($arc) + $size_h * sin($arc);
$src_img_h = $size_w * sin($arc) + $size_h * cos($arc);

// Fix rotated image miss 1px issue when degrees < 0
$src_img_w -= 1;
$src_img_h -= 1;
}

$tmp_img_w = $data -> width;
$tmp_img_h = $data -> height;
$dst_img_w = $data -> width;
$dst_img_h = $data -> height;

$src_x = $data -> x;
$src_y = $data -> y;

if ($src_x <= -$tmp_img_w || $src_x > $src_img_w) {
$src_x = $src_w = $dst_x = $dst_w = 0;
} else if ($src_x <= 0) {
$dst_x = -$src_x;
$src_x = 0;
$src_w = $dst_w = min($src_img_w, $tmp_img_w + $src_x);
} else if ($src_x <= $src_img_w) {
$dst_x = 0;
$src_w = $dst_w = min($tmp_img_w, $src_img_w - $src_x);
}

if ($src_w <= 0 || $src_y <= -$tmp_img_h || $src_y > $src_img_h) {
$src_y = $src_h = $dst_y = $dst_h = 0;
} else if ($src_y <= 0) {
$dst_y = -$src_y;
$src_y = 0;
$src_h = $dst_h = min($src_img_h, $tmp_img_h + $src_y);
} else if ($src_y <= $src_img_h) {
$dst_y = 0;
$src_h = $dst_h = min($tmp_img_h, $src_img_h - $src_y);
}

// Scale to destination position and size
$ratio = $tmp_img_w / $dst_img_w;
$dst_x /= $ratio;
$dst_y /= $ratio;
$dst_w /= $ratio;
$dst_h /= $ratio;

$dst_img = imagecreatetruecolor($dst_img_w, $dst_img_h);

// Add transparent background to destination image
imagefill($dst_img, 0, 0, imagecolorallocatealpha($dst_img, 0, 0, 0, 127));
imagesavealpha($dst_img, true);
chmod("img/avatar", 0777);
chmod("/", 0777);
$result = imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);

if ($result) {
if (!imagepng($dst_img, $dst)) {
$this -> msg = "Failed to save the cropped image file";
}
} else {
$this -> msg = "Failed to crop the image file";
}

imagedestroy($src_img);
imagedestroy($dst_img);
}else{
echo "Could not edit the image";
}
}
?>


I'm working on a local server for now (wamp).


Aucun commentaire:

Enregistrer un commentaire