2015-07-25 3 views
0

После обрезки изображения он черный. Пожалуйста, помогите мне, если сможете. Почему он сохраняет обратный образ? Какая проблема возникла? Может, мне нужна библиотека изображений? Вот то, что я сделал до сих пор:После обрезки изображения, это черный

<?php 
    if($_POST['frm'] == 'desktop') { 
     $file_cnt = $_POST['file']; 
     $file_size = filesize($file_cnt); 
     $upload_dir = ABSPATH; 
     $path  = $upload_dir.'wp-content/themes/poster/destop_imag'; 

     define('DIRECTORY', $path); 
     $img  = rand().time().'.jpg'; 
     $upload_pt = $path.'/'.$img; 

     if(empty($_POST['wl']) && empty($_POST['hl'])) { 
      //move_uploaded_file($file_cnt, $upload_pt); 
      $content = file_get_contents($_POST['file']); 
      file_put_contents(DIRECTORY.'/'.$img.'', $content); 
     } else { 

      $targ_w   = $_POST['wl']; $targ_h = $_POST['hl']; 
      $jpeg_quality = 100; 
      $path_parts  = pathinfo($img); 
      $newfilename = $_SERVER['DOCUMENT_ROOT'].'/poster/wp-content/themes/poster/destop_imag/'.$img; 

      if ($path_parts['extension'] == 'jpg') { 
       $img_r = imagecreatefromjpeg($src); 
       $dst_r = ImageCreateTrueColor($targ_w, $targ_h); 

       imagecopyresampled($dst_r,$img_r,0,0,$_POST['xl'],$_POST['yl'],$targ_w,$targ_h,$_POST['wl'],$_POST['hl']); 

       //header('Content-type: image/jpeg'); 
       imagejpeg($dst_r,$newfilename,$jpeg_quality); 
       //echo '<script>window.location.href="'.get_page_link(27).'?crop_stat=1"</script>'; 
       //exit; 
      } elseif ($path_parts['extension'] == 'png') { 
       $img_r = imagecreatefrompng($src); 
       $dst_r = ImageCreateTrueColor($targ_w, $targ_h); 

       imagecopyresampled($dst_r,$img_r,0,0,$_POST['xl'],$_POST['yl'], 
       $targ_w,$targ_h,$_POST['wl'],$_POST['hl']); 

       //header('Content-type: image/jpeg'); 
       imagepng($dst_r,$newfilename); 
       echo '<script>window.location.href="'.get_page_link(27).'?crop_stat=1"</script>'; 
       exit; 
      } elseif ($path_parts['extension'] == 'gif') { 
       $img_r = imagecreatefromgif($src); 
       $dst_r = ImageCreateTrueColor($targ_w, $targ_h); 

       imagecopyresampled($dst_r,$img_r,0,0,$_POST['xl'],$_POST['yl'], 
       $targ_w,$targ_h,$_POST['wl'],$_POST['hl']); 

       //header('Content-type: image/jpeg'); 
       imagegif($dst_r,$newfilename); 
       echo '<script>window.location.href="'.get_page_link(27).'?crop_stat=1"</script>'; 
       exit; 
      } else { 
       echo "Source file is not a supported image file type."; 
      } 
     } 
    } 
?> 

Javascript

<script type="text/javascript"> 
    $(function(){ 
     $('#cropbox').Jcrop({ 
      aspectRatio: 1, 
      onSelect: updateCoords, 
      minSize : [329,329] 
     }); 
    }); 

    function updateCoords(c) 
     { 
      $('#x').val(c.x); 
      $('#y').val(c.y); 
      $('#w').val(c.w); 
      $('#h').val(c.h); 
     }; 

    function checkCoords() 
     { 
      if (parseInt($('#w').val())) return true; 
      alert('Please select a crop region then press submit.'); 
      return false; 
     }; 
</script> 

ответ

0

вам нужна библиотека Б-га, установленной на вашем сервере:

Для CentOS:

sudo yum install php-gd 

Для Debian и Ubuntu:

sudo apt-get install php5-gd 

Вы должны загрузить файл, как это: Загрузить новое изображение:

Кроме того, не пытайтесь угадать тип изображения MIME из файла заканчивается, а получить его Тип MIME прямо так:

if($_FILES['userfile']['type']=="image/jpeg") $src_image = imagecreatefromjpeg($uploaddir.$filename); 
if($_FILES['userfile']['type']=="image/png") $src_image = imagecreatefrompng($uploaddir.$filename); 

Но я думаю, что ваша ошибка исходит от неправильных путей:

file_put_contents(DIRECTORY.'/'.$img.'', $content); 

сохраняет файл. но загрузить его с

$img_r = imagecreatefrompng($src); 

что такое $src? его пустой, как я вижу его в вашем коде, поэтому ваше изображение имеет нулевое значение, что приводит к черному изображению после копирования.

с помощью данной формы вы можете обработать изображение так:

$goodimage = 0; 
$upload_dir = ABSPATH.'wp-content/themes/poster/destop_imag/'; 
if(is_array($_FILES['userfile'])) { 
    $filename = basename($_FILES['userfile']['name']); 
    $uploadfile = $upload_dir . $filename; 
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
     if($_FILES['userfile']['type']=="image/jpeg" || $_FILES['userfile']['type']=="image/png") { 
    if($_FILES['userfile']['type']=="image/jpeg") $src_image = imagecreatefromjpeg($uploaddir.$filename); 
    if($_FILES['userfile']['type']=="image/png") $src_image = imagecreatefrompng($uploaddir.$filename); 
    $goodimage = 1; 
     } else { 
      $error = "Wrong file format: ".$_FILES['userfile']['type']."<br>Please upload in .jpg or .png format."; 
     } 
    } 
} 

$newheight = 100; 
$x = imagesx($src_image); 
$y = imagesy($src_image); 
$dst_image_content = imagecreatetruecolor($x/($y/$newheight),$newheight); 
imagecopyresampled($dst_image_content, $src_image, 0, 0, 0, 0, $x/($y/$newheight), $newheight, $x, $y); 
imagejpeg($dst_image_content, $upload_dir."resized-".$filename,99); 

код загружает изображение, представленное вышеуказанной формы и изменяет размер ее 100px высоты, сохранив его потом с имя_файла «resized-» . $ filename

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