2016-02-10 6 views
0

Я использую Jcrop для обрезки изображений от пользователя. Я следил за учебником здесь: http://deepliquid.com/content/Jcrop_Implementation_Theory.html. Вот мой кодОбрезать изображение с помощью imagecopyresampled + Jcrop

private function resizeImage($file_info) 
{ 
    $dst_w  = $dst_h  = 150; 
    $jpeg_quality = 90; 

    $src     = realpath($file_info['directory'] . '/' . $file_info['name']); 

    // CHMOD before cropping 
    chmod($src, 0777); 

    switch (strtolower($file_info['mime'])) { 
     case 'image/jpeg': 
      $src_img = imagecreatefromjpeg($src); 
      break; 
     case 'image/png': 
      $src_img = imagecreatefrompng($src); 
      break; 
     case 'image/gif': 
      $src_img = imagecreatefromgif($src); 
      break; 
     default: 
      exit('Unsupported type: ' . $file_info['mime']); 
    } 
    $dst_img = imagecreatetruecolor($dst_w, $dst_h); 

    // Resize and crop the image 
    imagecopyresampled(
     $dst_img, $src_img,     // Destination and Source image link resource 
     0, 0,        // Coordinate of destination point 
     $this->crop['x'], $this->crop['y'], // Coordinate of source point 
     $dst_w, $dst_h,      // Destination width and height 
     $this->crop['w'], $this->crop['h'] // Source width and height 
    ); 

    // Output the image 
    switch (strtolower($file_info['mime'])) { 
     case 'image/jpeg': 
      imagejpeg($dst_img, null, $jpeg_quality); 
      break; 
     case 'image/png': 
      imagepng($dst_img); 
      break; 
     case 'image/gif': 
      imagegif($dst_img); 
      break; 
     default: 
      exit('Unsupported type: ' . $file_info['mime']); 
    } 
} 

Я проверил как каталог, так и файл доступны для записи (разрешение 0777). Все переменные, которые я использую, также хорошо определены, ничего не ошибка. Однако окончательное изображение все еще находится в первоначальном виде, а не обрезано.

Никто не знает, почему он не работает? Любая помощь приветствуется.

ответ

1

вы используете list(), чтобы получить ширину и высоту исходного изображения, но вы не используете его в своем вызове imagecopyresampled(). Все остальное выглядит прекрасно, насколько я могу видеть, если это не исправить, проблема, вероятно, в другом месте класса.

вот мой способ обрезки для справки, если это помогает.

/** 
    * Crops the image to the given dimensions, at the given starting point 
    * defaults to the top left 
    * @param type $width 
    * @param type $height 
    * @param type $x 
    * @param type $y 
    */ 
    public function crop($new_width, $new_height, $x = 0, $y = 0){ 
     $img = imagecrop($this->image, array(
      "x" => $x, 
      "y" => $y, 
      "width" => $new_width, 
      "height" => $new_height 
     )); 

     $this->height = $new_height; 
     $this->width = $new_width; 

     if(false !== $img) $this->image = $img; 
     else self::Error("Could not crop image."); 

     return $this; 
    } 


изменения размеров метода, для справки ..

/** 
* resizes the image to the dimensions provided 
* @param type $width (of canvas) 
* @param type $height (of canvas) 
*/ 
public function resize($width, $height){ 
    // Resize the image 
    $thumb = imagecreatetruecolor($width, $height); 
    imagealphablending($thumb, false); 
    imagesavealpha($thumb, true); 
    imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height); 

    $this->image = $thumb; 
    $this->width = $width; 
    $this->height = $height; 

    return $this; 
} 
+0

Да, я забыл удалить список() строка хех. Спасибо за ваш комментарий к imagecrop, хотя еще не задокументирован в руководстве PHP, я определенно попробую его –

+0

Ой, делает ли imagecrop также размер? Я использую imagecopyresampled для обрезки и изменения размера изображения до 150x150 –

+0

. Без урожая не будет изменяться размер, но я добавил свой метод изменения размера, если это поможет. –

Смежные вопросы