2015-11-06 12 views
7

मेरी प्रोजेक्ट में मैं बस छवि वॉटरमार्किंग या छवि को गठबंधन करता हूं और इसके लिए कोड ठीक काम करता हूं।छवि मैनिप्ल्यूशन चौड़ाई और ऊंचाई सेटिंग

<!DOCTYPE html> 
<html> 
<head> 
<title>test</title> 
</head> 
<body> 
<?php 
if(isset($_POST['submit'])) 
{ 
// Give the Complete Path of the folder where you want to save the image  
$folder="uploads/"; 
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "$folder".$_FILES["fileToUpload"]["name"]); 
$file='uploads/'.$_FILES["fileToUpload"]["name"]; 

$uploadimage=$folder.$_FILES["fileToUpload"]["name"]; 
$newname= time(); 

$ext = pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION); 

// Set the thumbnail name 
$thumbnail = $folder.$newname.".".$ext; 
$imgname=$newname.".".$ext; 

// Load the mian image 
if ($ext=="png" || $ext=="PNG") { 
$source = imagecreatefrompng($uploadimage); 
} 
else if ($ext=="gif" || $ext=="GIF") { 
$source = imagecreatefromgif($uploadimage); 
} 
else if ($ext=="bmp" || $ext=="BMP") { 
$source = imagecreatefrombmp($uploadimage); 
} 
else{ 
$source = imagecreatefromjpeg($uploadimage); 
} 

// load the image you want to you want to be watermarked 
$watermark = imagecreatefrompng('uploads/logo1.png'); 

// get the width and height of the watermark image 
$water_width = imagesx($source)/2; 
$water_height = imagesy($watermark); 

// get the width and height of the main image image 
$main_width = imagesx($source); 
$main_height = imagesy($source); 

$im_middle_w = $main_width/2; 
$im_middle_h = $main_height/2; 

// Set the dimension of the area you want to place your watermark we use 0 
// from x-axis and 0 from y-axis 
$dime_x = $im_middle_w - $water_width/2; 
$dime_y = $im_middle_h - $water_height/2; 

// copy both the images 
imagecopy($source, $watermark, $dime_x, $dime_y, 0, 0, $water_width, $water_height); 

// Final processing Creating The Image 
imagejpeg($source, $thumbnail, 100); 
unlink($file); 
} 
?> 
<img src='uploads/<?php echo $imgname;?>'> 
</body> 
</html> 

लेकिन $ water_width स्थापित करने के साथ समस्या और मैं अपने स्रोत छवि के आधे के रूप में स्थापित करना चाहते हैं। लेकिन जब मेरे पास कम चौड़ाई या अधिक चौड़ाई की स्रोत छवि $ water_width की तुलना में होती है, तो इसे इस तरह सेट किया जाता है। स्रोत छवि चौड़ाई अधिक होने पर छवि देखें।

enter image description here और जब चौड़ाई कम है। enter image description here तो मेरी समस्या यह है कि $ water_width को स्रोत छवि चौड़ाई के रूप में कैसे सेट करें?

एलेक्स द्वारा आपका उत्तर यह इस तरह से आया है। enter image description here

+0

आपको पानी के निशान के रूप में क्या चाहिए? ** www.domain.com ** ?? @divyesh –

+0

हां। काले रंग की पृष्ठभूमि के बिना कई वाक्य प्रश्न में आखिरी छवि की तरह। –

+0

मुझे लगता है कि आप 'imagettftext' का उपयोग कर सकते हैं। मेरा जवाब जांचें। @Divyesh –

उत्तर

7

यह मूल छवि की आधी चौड़ाई के लिए वॉटरमार्क आकार बदलने और केंद्र में रख देगा:

// load the image you want to you want to be watermarked 
$watermark = imagecreatefrompng('uploads/logo1.png'); 

// get the width and height of the watermark image 
$water_width = imagesx($watermark); 
$water_height = imagesy($watermark); 

// get the width and height of the main image image 
$main_width = imagesx($source); 
$main_height = imagesy($source); 

// resize watermark to half-width of the image 
$new_height = round($water_height * $main_width/$water_width/2); 
$new_width = round($main_width/2); 
$new_watermark = imagecreatetruecolor($new_width, $new_height); 
// keep transparent background 
imagealphablending($new_watermark, false); 
imagesavealpha($new_watermark, true); 

imagecopyresampled($new_watermark, $watermark, 0, 0, 0, 0, $new_width, $new_height, $water_width, $water_height); 

// Set the dimension of the area you want to place your watermark we use 0 
// from x-axis and 0 from y-axis 
$dime_x = round(($main_width - $new_width)/2); 
$dime_y = round(($main_height - $new_height)/2); 

// copy both the images 
imagecopy($source, $new_watermark, $dime_x, $dime_y, 0, 0, $new_width, $new_height); 

// Final processing Creating The Image 
imagejpeg($source, $thumbnail, 100); 

imagedestroy($source); 
imagedestroy($watermark); 
imagedestroy($new_watermark); 
+0

मैं आपके उत्तर का उपयोग करके छवि दिखाता हूं यह काला पृष्ठभूमि प्रदर्शित करता है ... प्रश्न में अन्य छवि के रूप में इसे कैसे करें? –

+0

@ दीवीश जेसेडिया, मैंने वॉटरमार्क में अल्फा चैनल को संरक्षित करने के उत्तर में संशोधन किया है। एक साइड नोट के रूप में, पारदर्शिता प्रश्न का कई बार जवाब दिया गया है। जैसे http://stackoverflow.com/questions/32243/can-png-image-transparency-be-preserved-when-using-phps-gdlib-imagecopyresample। –

3

आप imagettftext विधि की कोशिश कर सकते हैं, अगर आप पारदर्शिता में किसी भी तरह के उच्च पूर्णता नहीं करना चाहती। आप इस कोड का प्रयास कर सकते हैं। आपको अपनी निर्देशिका में एक फ़ॉन्ट फ़ाइल सहेजनी है, यहां मैंने arial.ttf का उपयोग किया है।

$im = imagecreatefrompng("png.png"); //create image data 
$font = 'arial.ttf'; //font file name 
$randomString = "example.com"; //string need to be shown 
$main_width = imagesx($im);  //finding width and height 
$main_height = imagesy($im);  
$posx= $main_width/2; //finding center 
$posy = $main_height/2; 
$color = imagecolorallocate($im, 200, 200, 200); //Creating color 
$size = ($main_width/25)+1;  //determine size of font. +1 to avoid 0 
$temp = $size*5;  
$posx = $posx-$temp; //adjust to average center 
imagettftext($im,$size,0, $posx, $posy, $color, $font , $randomString); //apply a text 

आप अपने पाठ की स्थिति के लिए posx और posy समायोजित करने के लिए किया है। Size को कुछ तर्कों के साथ भी समायोजित किया जा सकता है।

$color = imagecolorallocate($im, 0, 0, 0); = काला

और $color = imagecolorallocate($im, 255, 255, 255); = सफेद।

आपको इसे अपने आवश्यक टेक्स्ट रंग के लिए समायोजित करना होगा।

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