2009-05-23 20 views
20

मैं PHP में एक छोटा सा फ़ंक्शन बनाना चाहता हूं जो रंग, आकार, पारदर्शिता इत्यादि जैसे तर्कों को लेता है और एक पीएनजी छवि आउटपुट करता है। मैं PHP जी.डी. पुस्तकालय के बारे में सुना है, लेकिन मुझे पता है कि कैसे एक के रूप में soon.media.mit.eduएक गतिशील पीएनजी छवि बनाएं

+0

लिंक नीचे .......... – Pacerier

+0

@Pacerier यह ऐसा कुछ देखा (https://web.archive.org/web/20090720063343/http://soon.media.mit.edu/masthead/images/hdr -research-landing.jpg) लेकिन रंग, पाठ और पारदर्शिता डी बदलती है प्रदत्त पैरामीटर पर लंबित। अब हमारे पास क्लाउडिनरी और इमेजिक्स जैसी सेवाएं हैं लेकिन फिर कुछ नया था! – apnerve

उत्तर

19

This is a good example रचनात्मक रूप में कुछ बना सकते हैं चाहते हैं, आप लगभग सब कुछ these functions उपयोग कर सकते हैं। संभव है, एक तुम बहुत मुश्किल होगा मैं हालांकि ढ़ाल, छोरों और रंग के साथ कुछ अजीब सामान किया है द्वारा वर्णित की तरह एक छवि बनाने।

यदि आप कुछ पैरामीटर के आधार पर गतिशील रूप से ऐसी छवि बनाना चाहते हैं तो आप फ़ोटोशॉप में पहले से ही छवियां बना सकते हैं और फिर उपयोगकर्ता द्वारा चुने गए उपयोगकर्ता के आधार पर उन्हें ओवरले कर सकते हैं।

आप हो सकता है बहुत मज़ा नहीं है।

संपादित करें: ओह, वैसे, अगर आपकी रुचि giving an invalid parameter shows some of the python code है जो छवि बनाने और त्रुटि उत्पन्न करने के लिए ज़िम्मेदार है। कोड के बारे में कुछ विचार पाने के लिए यह एक अच्छी जगह होगी।

2 संपादित करें: यह सिर्फ कुछ मैं प्रौद्योगिकी के इस तरह के साथ किया है है। ध्यान रखें कि यह थोड़ी देर पहले था। यह क्वेरी स्ट्रिंग के आधार पर एक नाम स्वीकार करता है और मूल रूप से कुछ यादृच्छिक संख्याओं के साथ कुछ लूप करता है।

यहां स्रोत कोड है, मैं किसी भी बेवकूफ कोड/उद्धरण के लिए क्षमा चाहता हूं। यह थोड़ी देर पहले लिखा गया था, जब मैं लगभग 14 वर्ष का था, मुझे विश्वास है (शायद कई त्रुटियां)।

<?php 
header("Content-type:image/jpeg"); 
$array=array("I am a monument to all your sins", "Currently making pizza","Best before 12/7/09", "Farming Onions"); 
     function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text) 
     { 
      // retrieve boundingbox 
      $bbox = imagettfbbox($size, $angle, $fontfile, $text); 
      // calculate deviation 
      $dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0;   // deviation left-right 
      $dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0;  // deviation top-bottom 
      // new pivotpoint 
      $px = $x-$dx; 
      $py = $y-$dy; 
      return imagettftext($im, $size, $angle, $px, $y, $color, $fontfile, $text); 
     } 
$image = imagecreate(500,90); 
$black = imagecolorallocate($image,0,0,0); 
$grey_shade = imagecolorallocate($image,40,40,40); 
$white = imagecolorallocate($image,255,255,255); 


$text = $array[rand(0,sizeof($array)-1)]; 

// Local font files, relative to script 
$otherFont = 'army1.ttf'; 
$font = 'army.ttf'; 

if($_GET['name'] == ""){ $name = "Sam152";}else{$name= $_GET['name'];} 
$name = substr($name, 0, 25);  


//BG text for Name 
while($i<10){ 
imagettftext_cr($image,rand(2,40),rand(0,50),rand(10,500),rand(0,200),$grey_shade,$font,$name); 
$i++; 
} 
//BG text for saying 
while($i<10){ 
imagettftext_cr($image,rand(0,40),rand(90,180),rand(100,500),rand(200,500),$grey_shade,$otherFont,$text); 
$i++; 
} 

// Main Text 
imagettftext_cr($image,35,0,250,46,$white,$font,$name); 
imagettftext_cr($image,10,0,250,76,$white,$otherFont,$text); 
imagejpeg($image); 

?> 
+1

हे! वह वास्तव में अच्छा था। मुझे कभी नहीं पता था कि इन कार्यों का उपयोग करके इसे हासिल किया जा सकता है। – apnerve

+0

ओह हाँ, यह हिमशैल की नोक है। वहां बहुत कुछ है। – Sam152

+0

मैंने सोचा कि उसने पीएनजी छवियों के बारे में पूछा है? –

6

यहां कोड है जिसे मैंने दो नामों वाली छवि उत्पन्न करने के लिए पहले उपयोग किया था, जो क्वेरी स्ट्रिंग पैरामीटर से स्वीकार किए जाते हैं। मैं एक तैयार पृष्ठभूमि छवि का उपयोग करता हूं और नामों को इसके ऊपर रखता हूं।

<?php 
// Print two names on the picture, which accepted by query string parameters. 

$n1 = $_GET['n1']; 
$n2 = $_GET['n2']; 

Header ("Content-type: image/jpeg"); 
$image = imageCreateFromJPEG("images/someimage.jpg"); 
$color = ImageColorAllocate($image, 255, 255, 255); 

// Calculate horizontal alignment for the names. 
$BoundingBox1 = imagettfbbox(13, 0, 'ITCKRIST.TTF', $n1); 
$boyX = ceil((125 - $BoundingBox1[2])/2); // lower left X coordinate for text 
$BoundingBox2 = imagettfbbox(13, 0, 'ITCKRIST.TTF', $n2); 
$girlX = ceil((107 - $BoundingBox2[2])/2); // lower left X coordinate for text 

// Write names. 
imagettftext($image, 13, 0, $boyX+25, 92, $color, 'ITCKRIST.TTF', $n1); 
imagettftext($image, 13, 0, $girlX+310, 92, $color, 'ITCKRIST.TTF', $n2); 

// Return output. 
ImageJPEG($image, NULL, 93); 
ImageDestroy($image); 
?> 

पृष्ठ पर निर्मित छवि प्रदर्शित करने के लिए आप कुछ इस तरह करते हैं:

<img src="myDynamicImage.php?n1=bebe&n2=jake" /> 
+0

वास्तव में मुझे यह देखने के लिए क्या देखना है कि यह कैसे करें। +1 – ThisBoyPerforms

4

नहीं "PHP में यह कर" लेकिन आप कुछ शक्तिशाली कमांड लाइन सॉफ्टवेयर कॉल कर सकते हैं के लिए एक सीधा जवाब PHP से। विशेष रूप से ImageMagick रसोई सिंक समेत सबकुछ आकर्षित करेगा। इसका "आउट ऑफ़ बैंड" प्रसंस्करण के लिए "बैक-एंड" स्क्रिप्ट्स के लिए उपलब्ध होने का लाभ भी है (यानी, अनुरोध पूरा होने के बाद छवि प्रसंस्करण निष्पादित करना (तेज उपयोगकर्ता प्रतिक्रिया) या देर रात रात बैच में संसाधन पीक समय।

2

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

function hexLighter($hex, $factor = 30) { 
    $new_hex = ''; 

    $base['R'] = hexdec($hex{0}.$hex{1}); 
    $base['G'] = hexdec($hex{2}.$hex{3}); 
    $base['B'] = hexdec($hex{4}.$hex{5}); 

    foreach ($base as $k => $v) { 
     $amount = 255 - $v; 
     $amount = $amount/100; 
     $amount = round($amount * $factor); 
     $new_decimal = $v + $amount; 

     $new_hex_component = dechex($new_decimal); 

     $new_hex .= sprintf('%02.2s', $new_hex_component); 
    } 

    return $new_hex; 
} 

// Sanitize/Validate provided color variable 
if (!isset($_GET['color']) || strlen($_GET['color']) != 6) { 
    header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request', true, 400); 

    exit(0); 
} 

if (file_exists("cache/{$_GET['color']}.png")) { 
    header('Content-Type: image/png'); 
    readfile("cache/{$_GET['color']}.png"); 

    exit(0); 
} 

// Desired final size of image 
$n_width = 50; 
$n_height = 50; 

// Actual size of source images 
$width = 125; 
$height = 125; 

$image = imagecreatetruecolor($width, $height); 
      imagesavealpha($image, true); 
      imagealphablending($image, false); 

$n_image = imagecreatetruecolor($n_width, $n_height); 
      imagesavealpha($n_image, true); 
      imagealphablending($n_image, false); 

$black = imagecolorallocate($image, 0, 0, 0); 
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127); 

imagefilledrectangle($image, 0, 0, $width, $height, $transparent); 

$layers = array(); 
$layers_processed = array(); 

$layers[] = array('src' => 'layer01.gif', 'level' => 0); // Border 
$layers[] = array('src' => 'layer02.gif', 'level' => 35);  // Background 
$layers[] = array('src' => 'layer03.gif', 'level' => 100); // White Quotes 

foreach ($layers as $idx => $layer) { 
    $img = imagecreatefromgif($layer['src']); 
    $processed = imagecreatetruecolor($width, $height); 

    imagesavealpha($processed, true); 
    imagealphablending($processed, false); 

    imagefilledrectangle($processed, 0, 0, $width, $height, $transparent); 

    $color = hexLighter($_GET['color'], $layer['level']); 
    $color = imagecolorallocate($image, 
     hexdec($color{0} . $color{1}), 
     hexdec($color{2} . $color{3}), 
     hexdec($color{4} . $color{5}) 
    ); 

    for ($x = 0; $x < $width; $x++) 
     for ($y = 0; $y < $height; $y++) 
      if ($black === imagecolorat($img, $x, $y)) 
       imagesetpixel($processed, $x, $y, $color); 

    imagecolortransparent($processed, $transparent); 
    imagealphablending($processed, true); 

    array_push($layers_processed, $processed); 

    imagedestroy($img); 
} 

foreach ($layers_processed as $processed) { 
    imagecopymerge($image, $processed, 0, 0, 0, 0, $width, $height, 100); 

    imagedestroy($processed); 
} 

imagealphablending($image, true); 

imagecopyresampled($n_image, $image, 0, 0, 0, 0, $n_width, $n_height, $width, $height); 

imagealphablending($n_image, true); 

header('Content-Type: image/png'); 
imagepng($n_image, "cache/{$_GET['color']}.png"); 
imagepng($n_image); 

// Free up memory 
imagedestroy($n_image); 
imagedestroy($image); 

आप इस बारे में अधिक जानकारी चाहते हैं। कोड, मेरे पास एक विस्तृत स्पष्टीकरण है मेरे blog पर ination।

3

यहाँ सरल उदाहरण है:

<?php 
    $your_text = "Helloooo Worldddd"; 

    $IMG = imagecreate(250, 80); 
    $background = imagecolorallocate($IMG, 0,0,255); 
    $text_color = imagecolorallocate($IMG, 255,255,0); 
    $line_color = imagecolorallocate($IMG, 128,255,0); 
    imagestring($IMG, 10, 1, 25, $your_text, $text_color); 
    imagesetthickness ($IMG, 5); 
    imageline($IMG, 30, 45, 165, 45, $line_color); 
    header("Content-type: image/png"); 
    imagepng($IMG); 
    imagecolordeallocate($IMG, $line_color); 
    imagecolordeallocate($IMG, $text_color); 
    imagecolordeallocate($IMG, $background); 
    imagedestroy($IMG); 
    exit; 
?> 
1

यहाँ गतिशील पाठ के साथ गतिशील png छवि बनाने के लिए मेरी समारोह है ... और

<img src="create_image.php?s=008080_F_1000_200&t=Sample%20Image%20Drawn%20By%20PHP" alt="GD Library Example Image" > 
यहाँ

create_image.php जो छवि प्रदान करता है अनुरोध किया जाता है जैसे- कहा जा सकता है ...

<?php 
$setting = isset($_GET['s']) ? $_GET['s'] : "FFF_111_100_100"; 
$setting = explode("_",$setting); 
$img = array(); 

switch ($n = count($setting)) { 
    case $n > 4 : 
    case 3: 
     $setting[3] = $setting[2]; 
    case 4: 
     $img['width'] = (int) $setting[2]; 
     $img['height'] = (int) $setting[3]; 
    case 2: 
     $img['color'] = $setting[1]; 
     $img['background'] = $setting[0]; 
     break; 
    default: 
     list($img['background'],$img['color'],$img['width'],$img['height']) = array('F','0',100,100); 
     break; 
} 

$background = explode(",",hex2rgb($img['background'])); 
$color = explode(",",hex2rgb($img['color'])); 
$width = empty($img['width']) ? 100 : $img['width']; 
$height = empty($img['height']) ? 100 : $img['height']; 
$string = (string) isset($_GET['t']) ? $_GET['t'] : $width ."x". $height; 

header("Content-Type: image/png"); 
$image = @imagecreate($width, $height) 
    or die("Cannot Initialize new GD image stream"); 

$background_color = imagecolorallocate($image, $background[0], $background[1], $background[2]); 
$text_color = imagecolorallocate($image, $color[0], $color[1], $color[2]); 

imagestring($image, 5, 5, 5, $string, $text_color); 
imagepng($image); 
imagedestroy($image); 

function hex2rgb($hex) { 
    // Copied 
    $hex = str_replace("#", "", $hex); 

    switch (strlen($hex)) { 
    case 1: 
     $hex = $hex.$hex; 
    case 2: 
      $r = hexdec($hex); 
      $g = hexdec($hex); 
      $b = hexdec($hex); 
     break; 

    case 3: 
      $r = hexdec(substr($hex,0,1).substr($hex,0,1)); 
      $g = hexdec(substr($hex,1,1).substr($hex,1,1)); 
      $b = hexdec(substr($hex,2,1).substr($hex,2,1)); 
     break; 

    default: 
      $r = hexdec(substr($hex,0,2)); 
      $g = hexdec(substr($hex,2,2)); 
      $b = hexdec(substr($hex,4,2)); 
     break; 
    } 

    $rgb = array($r, $g, $b); 
    return implode(",", $rgb); 
} 
0

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

<?php 
    $your_text = "Helloooo Worldddd"; 

    $IMG = imagecreate(250, 80); 
    $background = imagecolorallocate($IMG, 0,0,255); 
    $text_color = imagecolorallocate($IMG, 255,255,0); 
    $line_color = imagecolorallocate($IMG, 128,255,0); 
    imagestring($IMG, 10, 1, 25, $your_text, $text_color); 
    imagesetthickness ($IMG, 5); 
    imageline($IMG, 30, 45, 165, 45, $line_color); 
    header("Content-type: image/png"); 
    imagepng($IMG); 
    imagecolordeallocate($IMG, $line_color); 
    imagecolordeallocate($IMG, $text_color); 
    imagecolordeallocate($IMG, $background); 
    imagedestroy($IMG); 
    exit; 
?> 
संबंधित मुद्दे