Category: Programming
PHP-GD: Resize Transparent Image PNG & GIF
By default, you will get black background if you resize a transparent image. Play Daisy slots to see how good the quality of the background images are. To fix it, you need set alpha channel imagecolorallocatealpha to 127.
With imagecolorallocatealpha, it will allocate a color for an image.
Usage:
int imagecolorallocatealpha ( resource image, int red, int green, int blue, int alpha)
From PHP manual:
imagecolorallocatealpha() behaves identically to imagecolorallocate() with the addition of the transparency parameter alpha which may have a value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.
Returns FALSE if the allocation failed.
Before using it, you must set to false the blending mode for an image and set true the flag to save full alpha channel information.
Example:
<? $newImg = imagecreatetruecolor($nWidth, $nHeight); imagealphablending($newImg, false); imagesavealpha($newImg,true); $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127); imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent); imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]); ?>
Mysql String Functions
Name | Description |
---|---|
ASCII() |
Return numeric value of left-most character |
BIN() |
Return a string representation of the argument |
BIT_LENGTH() |
Return length of argument in bits |
CHAR_LENGTH() |
Return number of characters in argument |
CHAR() |
Return the character for each integer passed |
CHARACTER_LENGTH() |
A synonym for CHAR_LENGTH() |
CONCAT_WS() |
Return concatenate with separator |
CONCAT() |
Return concatenated string |
ELT() |
Return string at index number |
EXPORT_SET() |
Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string |
FIELD() |
Return the index (position) of the first argument in the subsequent arguments |
FIND_IN_SET() |
Return the index position of the first argument within the second argument |
FORMAT() |
Return a number formatted to specified number of decimal places |
HEX() |
Return a hexadecimal representation of a decimal or string value |
INSERT() |
Insert a substring at the specified position up to the specified number of characters |
INSTR() |
Return the index of the first occurrence of substring |
LCASE() |
Synonym for LOWER() |
LEFT() |
Return the leftmost number of characters as specified |
LENGTH() |
Return the length of a string in bytes |
LIKE |
Simple pattern matching |
LOAD_FILE() |
Load the named file |
LOCATE() |
Return the position of the first occurrence of substring |
LOWER() |
Return the argument in lowercase |
LPAD() |
Return the string argument, left-padded with the specified string |
LTRIM() |
Remove leading spaces |
MAKE_SET() |
Return a set of comma-separated strings that have the corresponding bit in bits set |
MATCH |
Perform full-text search |
MID() |
Return a substring starting from the specified position |
NOT LIKE |
Negation of simple pattern matching |
NOT REGEXP |
Negation of REGEXP |
OCTET_LENGTH() |
A synonym for LENGTH() |
ORD() |
Return character code for leftmost character of the argument |
POSITION() |
A synonym for LOCATE() |
QUOTE() |
Escape the argument for use in an SQL statement |
REGEXP |
Pattern matching using regular expressions |
REPEAT() |
Repeat a string the specified number of times |
REPLACE() |
Replace occurrences of a specified string |
REVERSE() |
Reverse the characters in a string |
RIGHT() |
Return the specified rightmost number of characters |
RLIKE |
Synonym for REGEXP |
RPAD() |
Append string the specified number of times |
RTRIM() |
Remove trailing spaces |
SOUNDEX() |
Return a soundex string |
SOUNDS LIKE (v4.1.0) |
Compare sounds |
SPACE() |
Return a string of the specified number of spaces |
STRCMP() |
Compare two strings |
SUBSTR() |
Return the substring as specified |
SUBSTRING_INDEX() |
Return a substring from a string before the specified number of occurrences of the delimiter |
SUBSTRING() |
Return the substring as specified |
TRIM() |
Remove leading and trailing spaces |
UCASE() |
Synonym for UPPER() |
UNHEX() (v4.1.2) |
Convert each pair of hexadecimal digits to a character |
UPPER() |
Convert to uppercase |
String-valued functions return NULL
if the length of the result would be greater than the value of the max_allowed_packet
system variable.
For functions that operate on string positions, the first position is numbered 1.
PHP: Procedural vs. Object Oriented Coding Style
PHP allows you to write code in two flavours, one is procedural and the other
is object oriented. You can even write procedural code in PHP5 and it will run
without any problems. If you are not clear about procedural and object oriented
programming, then we will have a look at these two different coding styles. The
following two examples are not fully running examples rather a pseudo code:
<?
$user_input = $_POST[‘field‘];
$filtered_content = filter($user_input);Â Â Â //user input filtering
mysql_connect(“dbhost”,”dbuser”,”dbpassword”);Â Â Â //database
mysql_select_db(“dbname”);
$sql = “some query”;
$result = mysql_query($sql);
while ($data = mysql_fetch_assoc())
{
process ($data);
}
process_user_input($filtered_content);
?>
You will notice using a lot of inline processing either directly or via using functions.
It may stand as an example of typical procedural operation. Let’s see how it looks
after converting it to OOP:
<?
$input_filter = new filter();
$input_filter->filter_user_input();Â Â Â //filter the user inputs
$db = new dal(“mysql”);Â Â Â //data access layer
$db->connect($dbconfig);Â Â //using mysql
$result = $db->execute($sql);
ReportGenerator::makereport($result);Â Â Â Â //process data
$model = new Postmodel($filter->get_filtered_content());
$model->insert();
?>
Now if you take a look into these two code snippets, you will find that the latter
one is much more readable. Well, you can make the first one more readable by
introducing some more functions into it, but how many functions are you ready
to search into when you use them? The latter snippet is better organized because
you know which object is handling which process. If you write big applications in
procedural style, it will be almost impossible to manage after a few versions. Of
course you can implement strict coding conventions, but it is agreed by millions
of developers that it won’t give you the ultimate manageability and usability if it’s
procedural unless you do it in OO style. Almost all big applications are written using
the object oriented approach.
Read More “PHP: Procedural vs. Object Oriented Coding Style”
Subdomain wildcard pada Ubuntu dengan bind9 – Part II
Menyambung artikel sebelumnya, kali ini kita akan membuat konfigurasi Virtual Host agar setiap request yang masuk langsung diarahkan ke domain utama.
Pertama, edit file konfigurasi default apache: /etc/apache2/sites-available/default
Ubah nilai NameVirtualHost dan VirtualHost menjadi *
$ sudo pico /etc/apache2/sites-available/default
sehingga menjadi:
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhostDocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory “/usr/lib/cgi-bin”>
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warnCustomLog /var/log/apache2/access.log combined
ServerSignature OnAlias /doc/ “/usr/share/doc/”
<Directory “/usr/share/doc/”>
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory></VirtualHost>
Kemudian buat file konfigurasi Virtual Host baru untuk domain kita.
$ sudo pico /etc/apache2/sites-available/situskita.tld
Kita akan menambahkan ServerAlias yang akan mengarahkan semua request *.situskita.tld ke domain utama.Dan arahkan DocumentRoot ke lokasi kerja anda.
Read More “Subdomain wildcard pada Ubuntu dengan bind9 – Part II”
Subdomain wildcard pada Ubuntu dengan bind9
Ada kasus seperti ini, kita akan membuat sebuah website komunitas yang nanti nya para pengunjung bisa mendaftar sebagai anggota. Dan setiap anggota masing-masing akan mendapatkan alamat URL: http://namauser.situskita.com/
Kelihatannya tampak mudah bukan?
Tinggal membuatkan Virtual Host untuk masing-masing anggota, dan daftarkan subdomainnya.
Namun permasalahannya adalah, kita ingin subdomain tersebut hanya sebagai identitas, dan bukan berupa direktori khusus untuk masing-masing anggota terdaftar. Dan yang lebih penting lagi, setiap yang baru mendaftar akan langsung mendapatkan alamat subdomainnya secara otomatis, tanpa campur tangan kita. Bayangkan jika ada 100 pengunjung yang mendaftar, dan semua itu harus kita buat secara manual. Akan sangat membuang waktu dan tenaga bukan?
Ini dibutuhkan jika kita ingin membuat situs komunitas, atau contohnya seperti situs jaringan pertemanan. Setiap anggota yang terdaftar akan mendapatkan alamat sendiri untuk menampilkan profil, blog, atau content lainnya.
Untuk mengatasi masalah itu kita akan menggunakan yang namanya “Subdomain Wildcard”. Dengan subdomain wildcard, kita hanya butuh 1 domain yang digunakan untuk memproses semua request subdomain. Artinya, semua yang xxx.domainutama.com, yyy.domainutama.com, zzz.domainutama.com akan diarahkan ke domainutama.com. Yang nantinya script PHP pada domainutama.com yang akan memprosesnya dan menampilkan halaman anggota tersebut.
Sebelumnya, kita akan membutuhkan DNS Server untuk membuat subdomain wildcard ini. Dengan membuat DNS Zone baru untuk domain yang akan digunakan.
Sebagai test untuk di mesin lokal, kita akan membuat domain bohongan yang hanya bisa jalan di jaringan lokal kita saja.
Domain yang digunakan: www.situskita.tld
IP Mesin kita: 192.168.1.2
Install paket bind9 untuk DNS Server nya (Jika belum ada)
$ sudo apt-get install bind9
setelah install, kita akan membuat DNS Zone baru. Jika anda sudah memiliki konfigurasi DNS sendiri, dan akan menggunakannya, silahkan lewati bagian ini. Lanjutkan ke tahap selanjutnya.
$ sudo pico /etc/bind/named.conf.local
Tambahkan baris berikut ini:
zone “situskita.tld” {
type master;
file “/etc/bind/db.situskita.tld”;
notify no;
};
Disini kita membuat zona baru untuk domain “situskita.tld”, dan konfigurasi nya terletak pada file “/etc/bind/db.situskita.tld”
Jika ingin menggunakan konfigurasi DNS zone yang sudah ada, anda hanya tinggal menambahkan baris berikut pada konfigurasi anda:
* A 192.168.1.2
Kalau belum ada, maka kita akan membuatnya.
$sudo pico /etc/bind/db.situskita.tld
Blogging via Google Docs
Selain untuk membuat dokumen, Google docs juga bisa untuk menulis blog, yang kemudian otomatis akan di posting di situs blog anda seperti: Blogger.com, livejournal.com, WordPress.com, dan lain nya. Bahkan untuk situs pribadi anda sendiri yang menggunakan engine yang didukung oleh API Google.
Kenapa harus Google Docs?
Kadang kala editor blog kita memiliki fitur yang terbatas. Dengan Google Docs, kita akan memiliki editor yang lumayan lengkap selayaknya Office Suite. Meskipun belum selengkap Office suite, namun Google Docs masih dalam tahap pengembangan. Dengan status BETA (khas Google) maka Google Docs bisa akan semakin lengkap lagi.
Selain itu, kita bisa mempublikasikan dokumen office kita tanpa perlu menulis ulang.
Contoh berikut menjelaskan penggunaan Google Docs untuk menulis blog pada situs pribadi yang menggunakan WordPress.
Langkah awal adalah anda harus mempunyai account pada Google. Apabila anda telah mempunyai alamat email pada Gmail, anda bisa menggunakannya.
Setelah login, klik pada menu “Documents” di sudut kiri atas tampilan Gmail anda, atau langsung ke http://docs.google.com/
Buat dokumen baru dengan memilih: “New -> Documents”
Akan muncul halaman baru dan anda bisa memulai menulis blog anda disini, lengkap dengan format tulisan.
Untuk memasukkan gambar, table, atau video, klik pada tab “insert“. Sama seperti anda membuat dokumen pada Office suite.
Jika telah selesai menulis, klik pada menu “File -> Save“. Untuk langsung mempublikasikannya ke blog anda, klik pada menu “Publish” di sudut kanan editor. Akan muncul halaman Publish.
Teka-teki Matematika
Mungkin sudah ada yang pernah mendapatkannya melalui email, tapi mari kita sebarkan lagi.
Saya memiliki file Excel yang ber-password. Nah untuk membukanya anda harus menjawab pertanyaan dibawah ini. Jawaban tersebut merupakan password untuk membuka file excel ini.
Petunjuknya adalah:
Dalam sebuah bus ada 7 orang gadis
Tiap gadis membawa 7 Tas
Tiap Tas berisi 7 Ekor kucing dewasa
Tiap Kucing dewasa menggendong 7 Anak Kucing
(Ingat!!!!!!!! Tiap kucing punya 4 kaki)
Pertanyaannya, Berapakah jumlah kaki yang ada dalam Bus?
Perhatikan baik-baik petunjuknya, jangan sampai terjebak. Supir, kondektur, dan atau lain-lainnya yang tidak disebut tidak perlu dihitung. Cukup yang ada di petunjuk saja.
Ini hanya soal matematika sederhana, dan hanya merupakan permainan kata-kata. Sehingga logika berperan cukup lumayan disini.
Jika jawaban anda tepat, anda akan bisa membuka file excel ini. Tuliskan nama anda di file excel ini, save, dan sebarkan ke teman-teman anda. (Bukan spam atau hoax loh. Juga bukan Virus.. cuma permainan saja).
nb: memang tidak ada yang istimewa didalamnya, hanya untuk iseng-iseng saja. Lumayan untuk mengasah otak kembali.
Perhatian:
- Jangan gunakan Password Cracker untuk membukanya!! Karena hal itu akan membuat percuma tujuan dari teka teki ini. Dan itu artinya anda tidak memiliki logika kuat untuk memecahkan masalah.
- Jika mengetahui jawabannya, JANGAN POSTING JAWABAN ANDA DISINI. Cukup tulis nama anda di file excel tersebut, dan sebarkan ke teman-teman.
- Ketika akan menyebarkannya kembali, pastikan file tersebut tetap berpassword. Dan JANGAN GANTI PASSWORDNYA!!
- Untuk pengguna Openoffice (atau editor lainnya selain MS-Office), anda akan menjumpai masalah ketika anda akan menyimpannya kembali dengan keadaan tetap berpassword. Karena file ini masih dalam format MS.Office, bukan Open Document Format (ODF). Jadi pastikan ketika anda menyimpannya, password masih tetap ada. M$ Suck!!