2014-03-01 1 views
3

내 웹 사이트에 스크린 샷을 업로드하고 있습니다. 너비와 높이가 같은 속성으로 원본 이미지를 업로드하고 싶습니다. 예를 들어 업로드하기 전의 이미지 크기가 3MB이고 업로드/저장 후 300KB가되어야하는 경우와 같이 크기를 줄이고 싶습니다.하지만 제 경우에는 이미지가 있습니다. 완전히 빈 이미지를 표시하지 않고 생성됩니다. 다음은 업로드시 이미지 크기 압축 -PHP

는 .. 내 파일을 업로드하기 위해 사용하고 코드입니다

<form action="" method="post" enctype="multipart/form-data" id="something" class="uniForm"> 
    <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" /> 
    <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button> 

<?php 
    if(isset($_POST['submit'])){ 
     if (isset ($_FILES['new_image'])){ 
      $imagename = $_FILES['new_image']['name']; 
      $source = $_FILES['new_image']['tmp_name']; 
      $target = "images/".$imagename; 
      move_uploaded_file($source, $target); 

      $imagepath = $imagename; 
      $save = "images/" . $imagepath; //This is the new file you saving 
      $file = "images/" . $imagepath; //This is the original file 

      list($width, $height) = getimagesize($file) ; 


      $tn = imagecreatetruecolor($width, $height) ; 
      $image = imagecreatefromjpeg($file) ; 
      imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height) ; 

      imagejpeg($tn, $save, 70) ; 

      $save = "images/sml_" . $imagepath; //This is the new file you saving 
      $file = "images/" . $imagepath; //This is the original file 

      list($width, $height) = getimagesize($file) ; 

      $modwidth = 130; 

      $diff = $width/$modwidth; 

      $modheight = 185; 
      $tn = imagecreatetruecolor($modwidth, $modheight) ; 
      $image = imagecreatefromjpeg($file) ; 
      imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

      imagejpeg($tn, $save, 70) ; 
     echo "Large image: <img src='images/".$imagepath."'><br>"; 
     echo "Thumbnail: <img src='images/sml_".$imagepath."'>"; 

     } 
    } ?> 

는 내가 모든 이미지 유형을 사용하려면 내가 JPG 이미지를 업로드하면 제대로 작동/그러나 다른 사람 작동하지 않습니다

답변

1

다음 코드와 설명을 사용하십시오. 형제

<?php 
function imageCreateFromAny($filepath) { 
    $type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize() 
    $allowedTypes = array(
     1, // [] gif 
     2, // [] jpg 
     3, // [] png 
     6 // [] bmp 
    ); 
    if (!in_array($type, $allowedTypes)) { 
     return false; 
    } 
    switch ($type) { 
     case 1 : 
      $im = imageCreateFromGif($filepath); 
     break; 
     case 2 : 
      $im = imageCreateFromJpeg($filepath); 
     break; 
     case 3 : 
      $im = imageCreateFromPng($filepath); 
     break; 
     case 6 : 
      $im = imageCreateFromBmp($filepath); 
     break; 
    } 
    return $im; 
} 
?> 
0
//This may not be the answer 

//in php we have different functions for different image formats so this may helps you 
imagecreatefromjpeg (string $filename) 
imagecreatefrompng (string $filename) 
imagecreatefromgif (string $filename) 

imagecreatefromjpeg DOC

imagecreatefrompng DOC

imagecreatefromgif DOC

+0

덕분에 나는 이미 1을 사용하고 난 정말 우리가 따라 조회 할 수 있도록 소스가 PNG 또는 다른 경우 유형을 검출 사용하는 방법을 KNW 해달라고 그들에게 를 KNW – user3177068