2012-11-06 15 views
7

मैं काफी उलझन में हूं कि पीडीजी छवियों का उपयोग जीडी लाइब्रेरी का उपयोग करके आकार में क्यों किया जाता है, मूल से आकार में बहुत बड़ा है।मूल छवि की तुलना में पीएनजी छवि का आकार इतना बड़ा क्यों है?

// create image from posted file 
$src = imagecreatefrompng($file['tmp_name']); 
// get original size of uploaded image 
list($width,$height) = getimagesize($file['tmp_name']); 
if($width>$maxImgWidth) { 
    // resize the image to maxImgWidth, maintain the original aspect ratio 
    $newwidth = $maxImgWidth; 
    $newheight=($height/$width)*$newwidth; 
    $newImage=imagecreatetruecolor($newwidth,$newheight); 

    // fill transparent with white 
    /*$white=imagecolorallocate($newImage, 255, 255, 255); 
    imagefill($newImage, 0, 0, $white);*/ 

    // the following is to keep PNG's alpha channels 
    // turn off transparency blending temporarily 
    imagealphablending($newImage, false); 
    // Fill the image with transparent color 
    $color = imagecolorallocatealpha($newImage,255,255,255,127); 
    imagefill($newImage, 0, 0, $color); 
    // restore transparency blending 
    imagesavealpha($newImage, true); 

    // do the image resizing by copying from the original into $newImage image 
    imagecopyresampled($newImage,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

    // write image to buffer and save in variable 
    ob_start(); // Stdout --> buffer 
    imagepng($newImage,NULL,5); // last parameter is compression 0-none 9-best (slow), see also http://www.php.net/manual/en/function.imagepng.php 
    $newImageToSave = ob_get_contents(); // store stdout in $newImageToSave 
    ob_end_clean(); // clear buffer 
    // remove images from php buffer 
    imagedestroy($src); 
    imagedestroy($newImage); 
    $resizedFlag = true; 
} 

तब मैं mysql डेटाबेस में ब्लॉब के रूप में $ newImageToSave बचाने:

इस कोड मैं छवि का आकार बदलने का उपयोग कर रहा है।

मैंने अल्फा चैनल को रोकने की कोशिश की और केवल सफेद पृष्ठभूमि सेट की, फ़ाइल आकार में कोई महत्वपूर्ण बदलाव नहीं हुआ। मैंने "संपीड़न" पैरामीटर (0 से 9) को सेट करने का प्रयास किया, लेकिन फिर मूल के बाद भी बड़ा।

उदाहरण
मैं ले लिया इस image (1058px * 1296px) और 900px * 1102px करने के लिए इसे आकार दिया।

मूल फ़ाइल:: 328 KB
पीएनजी (0): 3,79 एमबी
पीएनजी (5): 564 KB
पीएनजी (9): 503 KB

कोई टिप ये परिणाम हैं फ़ाइल आकार में छोटे आकार की छवि को कैसे प्राप्त किया जाए, इसकी सराहना की जाती है।

-

पुनश्च: मैंने सोचा कि यह बिट गहराई हो सकता है, लेकिन जैसा कि आप देख सकते हैं, उदाहरण के छवि के ऊपर, 32 बिट है, जबकि resized छवि 24 बिट है।

+1

आप उपयोग कर रहे हैं '5' संपीड़न कारक के लिए। '9' आज़माएं और देखें कि क्या होता है। –

+0

मुझे आश्चर्य है कि क्या आपका नया आयामी आकार संपीड़न को प्रभावी नहीं होने का कारण बन रहा है। यह देखना दिलचस्प होगा कि विभिन्न आकार आयाम किस फ़ाइल आकार में संपीड़ित होंगे। उदाहरण के लिए यदि लक्ष्य आयाम मूल आकार का एक आधा है, तो नया फ़ाइल आकार क्या है। –

+0

@MarcB बस ऊपर देखें: पीएनजी (9): 503 केबी –

उत्तर

10

आप छवियों को कम करने के लिए बुला रहे अधिकांश कार्यों में से अधिकांश नहीं हैं, imagefill, imagealphablending आदि उच्च फ़ाइल आकार के परिणामस्वरूप हो सकते हैं।

पारदर्शी उपयोग imagecreate बजाय imagecreatetruecolor बनाए रखने और सिर्फ एक सरल आकार

$file['tmp_name'] = "wiki.png"; 
$maxImgWidth = 900; 
// create image from posted file 
$src = imagecreatefrompng($file['tmp_name']); 
// get original size of uploaded image 
list($width, $height) = getimagesize($file['tmp_name']); 
if ($width > $maxImgWidth) { 
    $newwidth = $maxImgWidth; 
    $newheight = ($height/$width) * $newwidth; 
    $newImage = imagecreate($newwidth, $newheight); 
    imagecopyresampled($newImage, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
    imagepng($newImage, "wiki2.png", 5); 
    imagedestroy($src); 
    imagedestroy($newImage); 
    $resizedFlag = true; 
} 

अंतिम आकार कार्य करें: 164KB

+0

से नीचे एक समाधान मिल गया है, ठीक है। –

+0

बढ़िया, धन्यवाद! मूल: 328 केबी → आपके कोड के साथ आकार: 163 केबी। ... मुझे एकमात्र चीज नहीं मिलती है, पारदर्शी चैनल अब क्यों जीवित रहता है, कुछ पुराने कोड के साथ यह काला हो गया। –

+0

मेरी स्पष्टीकरण देखें ..... आप अब 'imagecreate' का उपयोग कर रहे हैं ... यह एक खाली स्लेट ... – Baba

संबंधित मुद्दे