में मैं फसल PHP में एक छवि पसंद है और फाइल को सेव चाहते हैं। मुझे पता है कि आप जीडी लाइब्रेरी का उपयोग करना चाहते हैं लेकिन मुझे यकीन नहीं है कि कैसे। कोई विचार?चित्र काट-छांट पीएचपी
धन्यवाद
में मैं फसल PHP में एक छवि पसंद है और फाइल को सेव चाहते हैं। मुझे पता है कि आप जीडी लाइब्रेरी का उपयोग करना चाहते हैं लेकिन मुझे यकीन नहीं है कि कैसे। कोई विचार?चित्र काट-छांट पीएचपी
धन्यवाद
आप imagecopy
किसी छवि का उपयोग के आवश्यक भाग के काटने के लिए कर सकता है। आदेश इस प्रकार है:
imagecopy (
resource $dst_im - the image object ,
resource $src_im - destination image ,
int $dst_x - x coordinate in the destination image (use 0) ,
int $dst_y - y coordinate in the destination image (use 0) ,
int $src_x - x coordinate in the source image you want to crop ,
int $src_y - y coordinate in the source image you want to crop ,
int $src_w - crop width ,
int $src_h - crop height
)
कोड PHP.net से - एक 80x40 पिक्सेल चित्र एक स्रोत छवि
<?php
// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
?>
से काटी है जी.डी. आप जी.डी. विधियों के संयोजन उपयोग करने की आवश्यकता का उपयोग कर एक छवि काटने के लिए, और यदि आप imagecopyresampled
विधि के PHP के दस्तावेज़ पर "उदाहरण # 1" देखते हैं, तो यह आपको दिखाता है कि छवि को कैसे फसल और आउटपुट करना है, आपको फ़ाइल को आउटपुट कैप्चर और लिखने के लिए बस कुछ कोड जोड़ना होगा .. ।
http://us2.php.net/manual/en/function.imagecopyresampled.php
भी Image Magick जो, अगर आपके सर्वर पर स्थापित, पीएचपी के exec
विधि (या समान) का उपयोग करते हुए सीधे पहुँचा जा सकता सहित अन्य विकल्प हैं या आप PHP Imagick विस्तार, जो उच्च गुणवत्ता वाले चित्रों पैदावार स्थापित कर सकते हैं और में, मेरी राय, काम करने के लिए थोड़ा और सहज और लचीला है।
अंत में, मैंने ओपन सोर्स PHPThumb क्लास लाइब्रेरी का उपयोग किया है, जिसमें एक बहुत ही सरल इंटरफ़ेस है और यह आपके सर्वर पर क्या है, इमेजमैजिक और जीडी समेत कई विकल्पों के साथ काम कर सकता है।
मैं कुछ परियोजनाओं में इस स्क्रिप्ट का उपयोग और इसका इस्तेमाल करने के लिए बहुत आसान है: http://shiftingpixel.com/2008/03/03/smart-image-resizer/
स्क्रिप्ट PHP 5.1.0 (जो बाद से 2005/11/24 बाहर है की आवश्यकता है - इस पर नहीं तो उन्नत करने के लिए अभी तक समय संस्करण) और जीडी (जो शायद ही कभी अच्छे वेब होस्ट से गायब है)।
<img src="/image.php/coffee-bean.jpg?width=200&height=200&image=/wp-content/uploads/2008/03/coffee-bean.jpg" alt="Coffee Bean" />
इस समारोह को बनाए रखने छवि पहलू अनुपात :)
function resize_image_crop($image, $width, $height)
{
$w = @imagesx($image); //current width
$h = @imagesy($image); //current height
if ((!$w) || (!$h)) { $GLOBALS['errors'][] = 'Image couldn\'t be resized because it wasn\'t a valid image.'; return false; }
if (($w == $width) && ($h == $height)) { return $image; } //no resizing needed
$ratio = $width/$w; //try max width first...
$new_w = $width;
$new_h = $h * $ratio;
if ($new_h < $height) { //if that created an image smaller than what we wanted, try the other way
$ratio = $height/$h;
$new_h = $height;
$new_w = $w * $ratio;
}
$image2 = imagecreatetruecolor ($new_w, $new_h);
imagecopyresampled($image2,$image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
if (($new_h != $height) || ($new_w != $width)) { //check to see if cropping needs to happen
$image3 = imagecreatetruecolor ($width, $height);
if ($new_h > $height) { //crop vertically
$extra = $new_h - $height;
$x = 0; //source x
$y = round($extra/2); //source y
imagecopyresampled($image3,$image2, 0, 0, $x, $y, $width, $height, $width, $height);
} else {
$extra = $new_w - $width;
$x = round($extra/2); //source x
$y = 0; //source y
imagecopyresampled($image3,$image2, 0, 0, $x, $y, $width, $height, $width, $height);
}
imagedestroy($image2);
return $image3;
} else {
return $image2;
}
}
कई imagecopyresampled कॉल करने की ज़रूरत नहीं करना चाहिए क्षण के लिए नीचे लगता है और $ ऊंचाई। इसके अलावा, यह सुनिश्चित करने के लिए कि आकार को अतिरिक्त कचरा और हेडर से अलग किए गए नए प्रारूप में सहेजा गया है और एक विशिष्ट गुणवत्ता के लिए मजबूर किया गया है, यह सुनिश्चित करने के लिए हमेशा आकार बदलने के लिए बेहतर हो सकता है। – Exit
आप चित्र काटने के लिए विधि नीचे का उपयोग कर सकते चित्र काटने के लिए होगा,
:यहाँ यह का एक उदाहरण आपके HTML में फायदा नहीं है है
/*parameters are
$image =source image name
$width = target width
$height = height of image
$scale = scale of image*/
function resizeImage($image,$width,$height,$scale) {
//generate new image height and width of source image
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
//Create a new true color image
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
//Create a new image from file
$source = imagecreatefromjpeg($image);
//Copy and resize part of an image with resampling
imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
//Output image to file
imagejpeg($newImage,$image,90);
//set rights on image file
chmod($image, 0777);
//return crop image
return $image;
}
यह उपयोगकर्ताओं के लिए उपयोगी हो सकता है यदि आप अपने कोड में टिप्पणियां जोड़ते हैं ताकि यह समझाने के लिए कि कौन से महत्वपूर्ण भाग हैं। इससे उन्हें आपके कोड – shrmn
ठीक से समझने और लागू करने में मदद मिलेगी, ठीक है, मैंने जोड़ा है –
मैंने अभी यह फ़ंक्शन बनाया है और यह मेरी आवश्यकताओं के लिए काम करता है, एक केंद्रित और फसल बना रहा है थंबनेल छवि। यह सुव्यवस्थित है और वेबगॉटम के उत्तर में दिखाए गए कई इमेजकोपी कॉल की आवश्यकता नहीं है।
छवि पथ, अंतिम चौड़ाई और ऊंचाई प्रदान करें, और छवि की गुणवत्ता को वैकल्पिक रूप से। मैंने इसे थंबनेल बनाने के लिए बनाया है, इसलिए सभी छवियों को जेपीजी के रूप में सहेजा जाता है, यदि आप उन्हें आवश्यकतानुसार अन्य छवि प्रकारों को समायोजित करने के लिए इसे संपादित कर सकते हैं। यहां मुख्य बिंदु एक थंबनेल बनाने के लिए imagecopyresampled का उपयोग करने के गणित और विधि है। छवियों को एक ही नाम, साथ ही छवि आकार का उपयोग करके सहेजा जाता है।
function resize_crop_image($image_path, $end_width, $end_height, $quality = '') {
if ($end_width < 1) $end_width = 100;
if ($end_height < 1) $end_height = 100;
if ($quality < 1 || $quality > 100) $quality = 60;
$image = false;
$dot = strrpos($image_path,'.');
$file = substr($image_path,0,$dot).'-'.$end_width.'x'.$end_height.'.jpg';
$ext = substr($image_path,$dot+1);
if ($ext == 'jpg' || $ext == 'jpeg') $image = @imagecreatefromjpeg($image_path);
elseif($ext == 'gif') $image = @imagecreatefromgif($image_path);
elseif($ext == 'png') $image = @imagecreatefrompng($image_path);
if ($image) {
$width = imagesx($image);
$height = imagesy($image);
$scale = max($end_width/$width, $end_height/$height);
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
$x = ($new_width != $end_width ? ($width - $end_width)/2 : 0);
$y = ($new_height != $end_height ? ($height - $end_height)/2 : 0);
$new_image = @imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image,$image,0,0,$x,$y,$new_width,$new_height,$width - $x,$height - $y);
imagedestroy($image);
imagejpeg($new_image,$file,$quality);
imagedestroy($new_image);
return $file;
}
return false;
}
त्वरित उत्तर के लिए धन्यवाद !! – user244228
इस विधि को पीएनजी छवियों में काला पृष्ठभूमि जोड़ें नहीं? मेरा मतलब पारदर्शी पृष्ठभूमि पर है? – bayblade567