2013-08-19 7 views
11

मैं थंबनेल फसल के लिए कल्पना का उपयोग करता हूं, लेकिन कभी-कभी फसल वाले थंबनेल छवियों (बालों, आंखों) के शीर्ष भाग को गायब कर रहे हैं।मैं php में कल्पना का उपयोग कैसे करूं? (आकार बदलें और फसल)

मैं छवि का आकार बदलने के बारे में सोच रहा था और फिर इसे फसल कर रहा था। इसके अलावा, मुझे छवि आकार अनुपात रखने की आवश्यकता है।

$im = new imagick("img/20130815233205-8.jpg"); 
$im->cropThumbnailImage(80, 80); 
$im->writeImage("thumb/th_80x80_test.jpg"); 
echo '<img src="thumb/th_80x80_test.jpg">'; 

धन्यवाद ..

+0

आपको क्या त्रुटियां मिल रही हैं? कितने उत्पादन की अपेक्षा है? PHP का संस्करण क्या है? कल्पना स्थापित है? अधिक जानकारी कृपया ... –

+1

नहीं, यह एक त्रुटि के बारे में नहीं है। कल्पना ठीक काम करता है। ऊपर लिपि केवल फसल है। मैं पहले रिज्यूज करना चाहता हूं, फिर मैं इसे फसल करना चाहता हूं, इसलिए मुझे पहला कदम याद आ रहा है .. – newworroo

+0

ठीक है, पहले 'imageResize'' पर कॉल करें, फिर ... –

उत्तर

22

इस कार्य के रूप में "महत्वपूर्ण" हिस्सा हमेशा एक ही स्थान पर नहीं हो सकता है आसान नहीं है:

नीचे PHP स्क्रिप्ट मैं फसल के लिए इस्तेमाल करते हैं। फिर भी, इस

$im = new imagick("c:\\temp\\523764_169105429888246_1540489537_n.jpg"); 
$imageprops = $im->getImageGeometry(); 
$width = $imageprops['width']; 
$height = $imageprops['height']; 
if($width > $height){ 
    $newHeight = 80; 
    $newWidth = (80/$height) * $width; 
}else{ 
    $newWidth = 80; 
    $newHeight = (80/$width) * $height; 
} 
$im->resizeImage($newWidth,$newHeight, imagick::FILTER_LANCZOS, 0.9, true); 
$im->cropImage (80,80,0,0); 
$im->writeImage("D:\\xampp\\htdocs\\th_80x80_test.jpg"); 
echo '<img src="th_80x80_test.jpg">'; 

(परीक्षण किया)

की तरह कुछ का उपयोग कर काम करना चाहिए। फसल छवि पैरामीटर (0 और 0) फसल क्षेत्र के ऊपरी बाएं कोने को निर्धारित करते हैं। तो इनके साथ खेलना आपको छवि में क्या रहता है इसके अलग-अलग परिणाम देता है।

/** 
* Resizes and crops $image to fit provided $width and $height. 
* 
* @param \Imagick $image 
* Image to change. 
* @param int $width 
* New desired width. 
* @param int $height 
* New desired height. 
*/ 
function image_cover(Imagick $image, $width, $height) { 
    $ratio = $width/$height; 

    // Original image dimensions. 
    $old_width = $image->getImageWidth(); 
    $old_height = $image->getImageHeight(); 
    $old_ratio = $old_width/$old_height; 

    // Determine new image dimensions to scale to. 
    // Also determine cropping coordinates. 
    if ($ratio > $old_ratio) { 
    $new_width = $width; 
    $new_height = $width/$old_width * $old_height; 
    $crop_x = 0; 
    $crop_y = intval(($new_height - $height)/2); 
    } 
    else { 
    $new_width = $height/$old_height * $old_width; 
    $new_height = $height; 
    $crop_x = intval(($new_width - $width)/2); 
    $crop_y = 0; 
    } 

    // Scale image to fit minimal of provided dimensions. 
    $image->resizeImage($new_width, $new_height, imagick::FILTER_LANCZOS, 0.9, true); 

    // Now crop image to exactly fit provided dimensions. 
    $image->cropImage($new_width, $new_height, $crop_x, $crop_y); 
} 

आशा इस किसी की मदद कर सकते:

+1

धन्यवाद !!! यह सही है .. – newworroo

1

पर Martin's answer आधार पर मैं एक अधिक सामान्य समारोह है कि आकार बदलता है और फसलों Imagick छवि को देखते हुए चौड़ाई और ऊंचाई फिट करने के लिए (अर्थात बिल्कुल के रूप में सीएसएस background-size: cover घोषणा बर्ताव करता है) बनाया है।

+1

'imagick :: FILTER_LANCZOS'' '\ Imagick :: FILTER_LANCZOS'' होना चाहिए –

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