PHP တွင်ကျွန်ုပ်တို့သည် GD စာကြည့်တိုက်ကို thumbnails များကိုထုတ်လုပ်ခြင်းအပါအ 0 င်ပုံများကိုလုပ်ဆောင်ရန်အသုံးပြုနိုင်သည်။ The Imagecolorallocatealealealpha function ကိုပွင့်လင်းမြင်သာသောရုပ်ပုံများသို့ပွင့်လင်းမြင်သာသောအရောင်များကိုသတ်မှတ်ပေးသည်။ နောက်တစ်ခုကပွင့်လင်းမြင်သာသောရုပ်ပုံများအတွက် thumbnails များကိုဖန်တီးရန်ဤလုပ်ဆောင်မှုကိုမည်သို့အသုံးပြုရမည်ကိုတစ်ဆင့်ချင်းတစ်ဆင့်ချင်းအလိုက်အဆင့်ဆင့်သွားပါလိမ့်မည်။
ပထမ ဦး စွာသင်၏ PHP ပတ်ဝန်းကျင်တွင် GD စာကြည့်တိုက်ကိုဖွင့်ထားကြောင်းသေချာပါစေ။ GD စာကြည့်တိုက်ကိုဖွင့်ထားခြင်းရှိမရှိစစ်ဆေးနိုင်သည်။
php -m | grep gd
GD စာကြည့်တိုက်ကို install လုပ်မထားပါကသင့်လျော်သော command ဖြင့် install လုပ်ပါ ။
ပွင့်လင်းမြင်သာသောပုံရိပ်အတွက်သမ်းနေးကိုဖန်တီးရန်သင်သည်မူလပုံကိုတင်ရန်လိုအပ်သည်။ ကျွန်ုပ်တို့သည် PNG (သို့) GIF ဖိုင်ကိုပွ င့ ်လင်းမြင်သာသောနောက်ခံဖြင့်ကိုင်တွယ်ဖြေရှင်းနေသည်ဟုယူဆသည် ။
$image = imagecreatefrompng('example.png');
ဤသည်ကိုလက်ရှိလမ်းညွှန်တွင်တည်ရှိသော ဥပမာ signer.png ဖိုင်ကို load လုပ်လိမ့်မည်။
သမ်းနေးကိုထုတ်လုပ်ရန်အတွက်ကျွန်ုပ်တို့သည်ပုံရိပ်ဗတ်စပုဒ်တစ်ခုဖန်တီးရန်နှင့်ပွင့်လင်းမြင်သာသောနောက်ခံရှိစေရန်လိုအပ်သည်။ ပွင့်လင်းမြင်သာမှုရှိစေရန်ကျွန်ုပ်တို့သည်မှန်ကန်သောပွင့်လင်းသောနောက်ခံကို ဦး စွာသတ်မှတ်ပြီး EmagEcolorallocatealpha ကို အသုံးပြု. ပွင့်လင်းမြင်သာသောအရောင်များကိုသတ်မှတ်ရမည်။
// မူရင်းပုံ၏အကျယ်နှင့်အမြင့်ကိုရယူပါ
$width = imagesx($image);
$height = imagesy($image);
// သမ်းနေး၏အကျယ်နှင့်အမြင့်ကိုသတ်မှတ်ပါ
$new_width = 100;
$new_height = 100;
// ပုံရိပ်အသစ်တစ်ခုကိုဖန်တီးပါ,နှင့်ပွင့်လင်းနောက်ခံအဖြစ်သတ်မှတ်
$thumb = imagecreatetruecolor($new_width, $new_height);
// အရောင်များကိုပွင့်လင်းသောနောက်ခံသို့သတ်မှတ်ပါ
$transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127); // 0, 0, 0 အနက်ရောင်ပါ,127 ပြည့်စုံ Transparency ဖော်ပြသည်
imagefill($thumb, 0, 0, $transparent);
// ရုပ်ပုံ Transparency ကို Enable လုပ်ပါ
imagesavealpha($thumb, true);
ဤကုဒ်တွင် imagecolorallocatealpha ($ thumb, 0. , 0, 0, 0, 0, 127) သည်ပွ င့ ်လင်းမြင်သာသောနောက်ခံအရောင်များကို Thumbnails သို့သတ်မှတ်ရန်အသုံးပြုသည်။
ထို့နောက်ကျွန်ုပ်တို့သည် ISASECOPYRESSAMPLED function ကို အသုံးပြု. မူလပုံကိုပတ်တူပေါ်သို့စကေးစေရန်အသုံးပြုသည်။
// စကေးကို thumbnail ပတ်တူသို့မူရင်းပုံရိပ်ကိုကူးယူပါ
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
သမ်းနေးကိုထုတ်လုပ်သည်နှင့်တပြိုင်နက်၎င်းကိုဖိုင်တစ်ခုသို့သိမ်းဆည်းရန်သို့မဟုတ် browser သို့တိုက်ရိုက်ထုတ်ရန်ရွေးချယ်နိုင်သည်။
// ဖိုင်အဖြစ် thumbnail များကိုသိမ်းဆည်းပါ
imagepng($thumb, 'thumb_example.png');
// သို့မဟုတ် browser ကိုတိုက်ရိုက် output ကို
header('Content-Type: image/png');
imagepng($thumb);
// မှတ်ဉာဏ်သန့်ရှင်းရေး
imagedestroy($image);
imagedestroy($thumb);
ဤတွင်အပြည့်အဝကုဒ်ဥပမာတစ်ခုဖြစ်သည်:
<?php
// မူရင်းပုံကိုတင်ပါ
$image = imagecreatefrompng('example.png');
// မူရင်းပုံ၏အကျယ်နှင့်အမြင့်ကိုရယူပါ
$width = imagesx($image);
$height = imagesy($image);
// သမ်းနေး၏အကျယ်နှင့်အမြင့်ကိုသတ်မှတ်ပါ
$new_width = 100;
$new_height = 100;
// ပုံရိပ်အသစ်တစ်ခုကိုဖန်တီးပါ,နှင့်ပွင့်လင်းနောက်ခံအဖြစ်သတ်မှတ်
$thumb = imagecreatetruecolor($new_width, $new_height);
// အရောင်များကိုပွင့်လင်းသောနောက်ခံသို့သတ်မှတ်ပါ
$transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127); // 0, 0, 0 အနက်ရောင်ပါ,127 ပြည့်စုံ Transparency ဖော်ပြသည်
imagefill($thumb, 0, 0, $transparent);
// ရုပ်ပုံ Transparency ကို Enable လုပ်ပါ
imagesavealpha($thumb, true);
// စကေးကို thumbnail ပတ်တူသို့မူရင်းပုံရိပ်ကိုကူးယူပါ
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// ဖိုင်အဖြစ် thumbnail များကိုသိမ်းဆည်းပါ
imagepng($thumb, 'thumb_example.png');
// သို့မဟုတ် browser ကိုတိုက်ရိုက် output ကို
// header('Content-Type: image/png');
// imagepng($thumb);
// မှတ်ဉာဏ်သန့်ရှင်းရေး
imagedestroy($image);
imagedestroy($thumb);
?>