2010-07-15 10 views
11

खोने के बिना चौड़ाई और ऊंचाई को ठीक करने के लिए मैं थंबनेल बनाने की तलाश में हूं जिसमें 100px आयाम द्वारा 100px है। मैंने विधियों को समझाते हुए कई लेख देखे हैं, लेकिन चौड़ाई होने पर सबसे अधिक अंत! = ऊंचाई अगर आयाम अनुपात रखा जाना है।PHP फसल छवि आयाम अनुपात

उदाहरण के लिए, मेरे पास 350px छवि द्वारा 450px है। मैं 100px द्वारा 100px तक फसल करना चाहता हूं। अगर मैं अनुपात रखना चाहता था, तो मैं 77px द्वारा 100px होने वाला अंत करूँगा। यह एक छवि और पंक्ति में इन छवियों को सूचीबद्ध करते समय यह बदसूरत बनाता है। हालांकि, आयाम अनुपात के बिना एक छवि भी भयानक लगती है।

मैंने फ़्लिकर से छवियां देखी हैं और वे शानदार लगती हैं। उदाहरण के लिए:
थंबनेल: http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
मध्यम आकार: http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
बड़े आकार: http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg

टी.के.एस

+0

ओह अब मुझे पता है कि किसी ने मुझे उस बी 4 के लिए क्यों डांटा। मुझे टिक साइन पर क्लिक करना चाहिए !! ओमजी मैं इसे ध्यान में रखता हूँ। ठीक है मुझे जवाब – nuttynibbles

उत्तर

34

यह केवल थंबनेल जो एक 1 है के रूप में छवि का एक हिस्सा का उपयोग करके किया जाता है: 1 पहलू अनुपात (ज्यादातर छवि का केंद्र)। यदि आप बारीकी से देखते हैं तो आप इसे फ़्लिकर थंबनेल में देख सकते हैं।

क्योंकि आपके प्रश्न में "फसल" है, मुझे यकीन नहीं है कि क्या आपको पहले से ही यह पता नहीं था, लेकिन आप तब क्या जानना चाहते हैं?

फसल का उपयोग करने के लिए, यहाँ एक उदाहरण है:

//Your Image 
$imgSrc = "image.jpg"; 

//getting the image dimensions 
list($width, $height) = getimagesize($imgSrc); 

//saving the image into memory (for manipulation with GD Library) 
$myImage = imagecreatefromjpeg($imgSrc); 

// calculating the part of the image to use for thumbnail 
if ($width > $height) { 
    $y = 0; 
    $x = ($width - $height)/2; 
    $smallestSide = $height; 
} else { 
    $x = 0; 
    $y = ($height - $width)/2; 
    $smallestSide = $width; 
} 

// copying the part into thumbnail 
$thumbSize = 100; 
$thumb = imagecreatetruecolor($thumbSize, $thumbSize); 
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide); 

//final output 
header('Content-type: image/jpeg'); 
imagejpeg($thumb); 
+0

हाय स्कोस्केनिक * डबल चेक करें यदि मैंने आपका नाम सही ढंग से लिखा है =) * जो मेरे दिमाग को पार कर गया है। लेकिन मैं यह कैसे करने के बारे में अनजान हूं। साझा करने के लिए परवाह?? – nuttynibbles

+0

हे स्वेन, मैंने आपकी स्क्रिप्ट की कोशिश की और यह बहुत अच्छा काम करता है। आप बहुत टीके। दोषपूर्ण – nuttynibbles

+0

हे स्वेन, मेरे पास सिर्फ एक सवाल है। यूआर स्क्रिप्ट का उपयोग करना बहुत अच्छा काम करता है। लेकिन मुझे एहसास हुआ कि फसल के बाद छवि की गुणवत्ता मूल के समान नहीं है। उदाहरण के लिए, मैंने फ़्लिकर और फसल से एक ही आयाम में नमूना चित्र लिया और वे अलग दिखते हैं। गुणवत्ता बनाए रखने के लिए कोई समाधान ?? – nuttynibbles

0

मैं GDLib का उपयोग कर पसंद छवियों के साथ बेला के लिए, यह भी साथ काम करने सनक से आसान है। वहाँ बहुत से ब्लॉग पोस्ट और ट्यूटोरियल हैं। मैं इसके लिए एक वर्ग का उपयोग करने की सिफारिश करता हूं या इसी तरह, जैसा कि छवि में सभी भिन्नताओं का ख्याल रखना बहुत समय ले सकता है। कम चौड़ाई या ऊंचाई

public function croppThis($target_url) { 

    $this->jpegImgCrop($target_url); 

} 

$ target_url के आधार पर वर्ग के साथ

+0

शायद इसके साथ क्या चल रहा है? http://pear.php.net/package/Image_Transform –

3

फसल छवि - छवि का नाम है।

public function jpegImgCrop($target_url) {//support 



    $image = imagecreatefromjpeg($target_url); 
    $filename = $target_url; 
    $width = imagesx($image); 
    $height = imagesy($image); 
    $image_type = imagetypes($image); //IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM 

    if($width==$height) { 

    $thumb_width = $width; 
    $thumb_height = $height; 

    } elseif($width<$height) { 

    $thumb_width = $width; 
    $thumb_height = $width; 

    } elseif($width>$height) { 

    $thumb_width = $height; 
    $thumb_height = $height; 

    } else { 
    $thumb_width = 150; 
    $thumb_height = 150; 
    } 

    $original_aspect = $width/$height; 
    $thumb_aspect = $thumb_width/$thumb_height; 

    if ($original_aspect >= $thumb_aspect) { 

    // If image is wider than thumbnail (in aspect ratio sense) 
    $new_height = $thumb_height; 
    $new_width = $width/($height/$thumb_height); 

    } 
    else { 
    // If the thumbnail is wider than the image 
    $new_width = $thumb_width; 
    $new_height = $height/($width/$thumb_width); 
    } 

    $thumb = imagecreatetruecolor($thumb_width, $thumb_height); 

    // Resize and crop 
    imagecopyresampled($thumb, 
     $image, 
     0 - ($new_width - $thumb_width)/2, // Center the image horizontally 
     0 - ($new_height - $thumb_height)/2, // Center the image vertically 
     0, 0, 
     $new_width, $new_height, 
     $width, $height); 
    imagejpeg($thumb, $filename, 80); 

} 
+0

आसान कामकाजी समाधान; लेकिन पहले फ़ाइल प्रकार की जांच करें और jpg, png या gif imagecreatefrom का उपयोग थोड़ा और सहायक हो सकता है। – web2kx

+0

यह कैसे काम करें .. कृपया मेरी मदद करें .. छवि की तरह कैसे दिखें – user3501407

3

आप इस कोड का उपयोग कर सकते हैं। आपको पीएक्स में स्रोत छवि पथ और थंबनेल आकार, और वैकल्पिक गंतव्य पथ पास करने की आवश्यकता है। यदि आप पास करते हैं तो यह छवि को सहेज लेगा और अंगूठे दिखाया जाएगा।

केवल jpg, jpeg और png की अनुमति है।

function cropImage($sourcePath, $thumbSize, $destination = null) { 

    $parts = explode('.', $sourcePath); 
    $ext = $parts[count($parts) - 1]; 
    if ($ext == 'jpg' || $ext == 'jpeg') { 
    $format = 'jpg'; 
    } else { 
    $format = 'png'; 
    } 

    if ($format == 'jpg') { 
    $sourceImage = imagecreatefromjpeg($sourcePath); 
    } 
    if ($format == 'png') { 
    $sourceImage = imagecreatefrompng($sourcePath); 
    } 

    list($srcWidth, $srcHeight) = getimagesize($sourcePath); 

    // calculating the part of the image to use for thumbnail 
    if ($srcWidth > $srcHeight) { 
    $y = 0; 
    $x = ($srcWidth - $srcHeight)/2; 
    $smallestSide = $srcHeight; 
    } else { 
    $x = 0; 
    $y = ($srcHeight - $srcWidth)/2; 
    $smallestSide = $srcWidth; 
    } 

    $destinationImage = imagecreatetruecolor($thumbSize, $thumbSize); 
    imagecopyresampled($destinationImage, $sourceImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide); 

    if ($destination == null) { 
    header('Content-Type: image/jpeg'); 
    if ($format == 'jpg') { 
     imagejpeg($destinationImage, null, 100); 
    } 
    if ($format == 'png') { 
     imagejpeg($destinationImage); 
    } 
    if ($destination = null) { 
    } 
    } else { 
    if ($format == 'jpg') { 
     imagejpeg($destinationImage, $destination, 100); 
    } 
    if ($format == 'png') { 
     imagepng($destinationImage, $destination); 
    } 
    } 
} 
+1

यह अंगूठे बना रहा है? अंगूठे पथ क्या है? कुछ विवरण भी लिखें। –

+1

@ पाथिक वेजानी धन्यवाद, मैंने कुछ विवरण जोड़ा। –