upload görüntü dosyası boyutunu sıkıştırmak?

5 Cevap php

I am uploading product screenshots to my website... I want to upload the original image as it is and also create a thumbnail for it, but after uploading both the files the filesize of the thumbnail created is a bit larger than expected. Is there any way I could reduce the filesize of the thumbnail without compromising much on the quality in php or by using Imagemagick( which I have no idea how to use but I'm willing to learn if needed)...
Below is the code I'm using to upload my files..

<form action="<?php echo $_server['php-self'];  ?>" 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, 100) ; 

          $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, 100) ; 
        echo "Large image: <img src='images/".$imagepath."'><br>"; 
        echo "Thumbnail: <img src='images/sml_".$imagepath."'>"; 

      }
    } ?>

Lütfen bana doğru yönde işaret ... Teşekkürler

5 Cevap

Için kalitesi olarak 100 geçemiyor imagejpeg() - 90 üzerinde bir şey genellikle overkill ve sadece bir büyük JPEG alır. Bir küçük resim için, 75 deneyin ve kalite / boyut Tradeoff kabul kadar aşağı çalışır.

//try this
imagejpeg($tn, $save, 75) ;

Bu 60 olmalı, o yüzde 60 için duruyor.

Example: If you open an image in Photoshop and try save it for web and select jpg, you can see that by using 60 it's still under high quality, but lower file size. If you would like lower, with more degradation, meaning the colors are distorted more.

60'tan fazla daha iyi bir şey, sadece daha büyük dosya boyutu vermez.

Bu web standart görüntü optimizasyonu bulunuyor. Yüksek kalitede tutmak ama mümkün olduğunca düşük dosya boyutu tutmak.

% 100 A kalite ayarı oldukça büyük için ist. Eğer alışkanlık görüntülerin çoğu bir fark görmek 85 veya% 90 deneyin.

see: http://www.ampsoft.net/webdesign-l/jpeg-compression.html

75 Eğer bunu kullanırsanız ancak oldukça kaliteli bir düşüş fark edeceksiniz, varsayılan kalite ayarıdır. 90 size harika bir görüntü kalitesi sağlar ve yarım dosya boyutunu azaltır Eğer dosya boyutunu azaltmak istiyorsanız daha o feryat 85 veya 80 ama hiçbir şey kullanın.

75 ve 85 arasında bir kalite kullanarak, jpeg kullanarak genellikle daha kabul edilebilir (100 btw, o önemli değil, bir kazanç için, yol çok fazla yer kaplıyor), en az fotoğraflar için ise.

Eğer ekran ile ilgili ise bir dipnot düşmek gibi, PNG kullanarak size daha kaliteli alabilirsiniz: jpeg görüntüleri düşürür (it is quite OK for photos, but for screenshots, with fonts that are drawn by pixels, it can bring some not nice-looking effect)