Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, February 1, 2016

Image Thumbnails Using PHP

function make_thumb($src, $dest, $desired_width) {

 /* read the source image */
 $source_image = imagecreatefromjpeg($src);
 $width = imagesx($source_image);
 $height = imagesy($source_image);
 
 /* find the "desired height" of this thumbnail, relative to the desired width  */
 $desired_height = floor($height * ($desired_width / $width));
 
 /* create a new, "virtual" image */
 $virtual_image = imagecreatetruecolor($desired_width, $desired_height);
 
 /* copy source image at a resized size */
 imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
 
 /* create the physical thumbnail image to its destination */
 imagejpeg($virtual_image, $dest);
}

Friday, August 24, 2012

Don’t use short tags in PHP

Don’t use short tags and try to using , It can create a problem for you if you are going to deploy your application on another server.

Monday, August 20, 2012

PHP Regular Expressions

Source: http://www.roscripts.com/PHP_regular_expressions_examples-136.html


//Credit card: All major cards
'^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$'

//Credit card: American Express
'^3[47][0-9]{13}$'

//Credit card: Diners Club
'^3(?:0[0-5]|[68][0-9])[0-9]{11}$'

//Credit card: Discover
'^6011[0-9]{12}$'

//Credit card: MasterCard
'^5[1-5][0-9]{14}$'

//Credit card: Visa
'^4[0-9]{12}(?:[0-9]{3})?$'

//Credit card: remove non-digits
'/[^0-9]+/'

Online Regular Expression Checker
http://gskinner.com/RegExr/

http://cybernetnews.com/online-regular-expression-builder/

Sunday, August 19, 2012

PHP Year 2038 problem


What exactly is the Year 2038 problem?

"The year 2038 problem (also known as Unix Millennium Bug, Y2K38 by analogy to the Y2K problem) may cause some computer software to fail before or in the year 2038. The problem affects all software and systems that store system time as a signed 32-bit integer, and interpret this number as the number of seconds since 00:00:00 UTC on January 1, 1970."

Tuesday, August 7, 2012

Collecting Garbage: PHP's take on variables


Source: http://derickrethans.nl/collecting-garbage-phps-take-on-variables.html

In this three part column I will explain the merits of the new Garbage Collection (also known as GC) mechanism that is part of PHP 5.3. Before we start with the intricate details of PHP's new GC engine I will explain why it is actually needed. This, combined with an introduction how PHP deals with variables in general is explained in this first part of the column. The second part will cover the solution and some notes on the GC mechanism itself, and the third part covers some implications of the GC mechanism, as well as some benchmarks. But now first on to the introduction.

PHP stores variables in containers called a "zval". A zval container contains besides the variable's type and value, also two additional bits of information. The first one is called "is_ref" and contains a boolean value whether this variable is part of a "reference set". With this bit PHP's engine knows how to differentiate between normal variables, and references. However, PHP has user-land references—as created by the & operator, but also an internal reference counting mechanism to optimize memory usage. The second piece of additional information, called "refcount", contains how many variables names—also called symbols—point to this one zval container. All symbols are stored in a symbol table, of which there is one per scope. There is a scope for the main script (ie, the one requested through the browser), as well as for every function or method.

A zval container is created when a new variable is created with a constant value, such as:

$a = "new string";

In this case the new symbol name "a" is created in the current scope, and a new variable container is created with type "string", value "new string". The "is_ref" bit is by default set to "false" because no user-land reference has been created. The "refcount" is set to "1" as there is only one symbol that makes use if this variable container. Also, if the "refcount" is "1", "is_ref" is always "false". If you have Xdebug installed you can display this information by calling:

xdebug_debug_zval('a');

which displays:

a: (refcount=1, is_ref=0)='new string'

Assigning this variable to another variable name, increases the refcount:

$a = "new string";
$b = $a;
xdebug_debug_zval( 'a' );

which displays:

a: (refcount=2, is_ref=0)='new string'

The refcount is "2" here, because the same variable container is linked with both "a" and "b". PHP is smart enough not to copy the actual variable container when it is not necessary. Variable containers get destroyed when the "refcount" reaches zero. The "refcount" gets decreased by one for each symbol linked to the variable container leaves the scope (f.e. if the function ends) or when unset() is called on a symbol. The following example shows that:

$a = "new string";
$c = $b = $a;
xdebug_debug_zval( 'a' );
unset( $b, $c );
xdebug_debug_zval( 'a' );

which displays:

a: (refcount=3, is_ref=0)='new string'
a: (refcount=1, is_ref=0)='new string'

If we now call "unset( $a );" the variable container, including the type and value will be removed from memory.

Things get a tad more complex with compound types such as arrays and objects. Instead of a scalar value, arrays and objects store their properties in a symbol table of their own. This means that the following example creates three zval containers:

$a = array( 'meaning' => 'life', 'number' => 42 );
xdebug_debug_zval( 'a' );

which displays (after formatting):

a: (refcount=1, is_ref=0)=array (
        'meaning' => (refcount=1, is_ref=0)='life',
        'number' => (refcount=1, is_ref=0)=42
)

Graphically, it looks like:











You can see the three zval containers here: "a", "meaning" and "number". Similar rules apply for increasing and decreasing "refcounts". Below we add another element to the array, and set it's value to the contains of an already existing element:

$a = array( 'meaning' => 'life', 'number' => 42 );
$a['life'] = $a['meaning'];
xdebug_debug_zval( 'a' );

which displays (after formatting):

a: (refcount=1, is_ref=0)=array (
        'meaning' => (refcount=2, is_ref=0)='life',
        'number' => (refcount=1, is_ref=0)=42,
        'life' => (refcount=2, is_ref=0)='life'
)

Graphically, it looks like:











From the above Xdebug output,we see that both the old and new array elements now point to a zval container whose "refcount" is "2". Although Xdebug's output shows two zval containers with value "life", they are the same one. The The function xdebug_debug_zval() function does not show this, but you could see it by also displaying the memory pointer.

Removing an element from the array is like removing a symbol from a scope. By doing so, the "refcount" of a container that an array element points to is decreased. Again when the "refcount" reaches zero, the variable container is removed from memory. Again an example to show this:

$a = array( 'meaning' => 'life', 'number' => 42 );
$a['life'] = $a['meaning'];
unset( $a['meaning'], $a['number'] );
xdebug_debug_zval( 'a' );

which displays (after formatting):

a: (refcount=1, is_ref=0)=array (
        'life' => (refcount=1, is_ref=0)='life'
)

Now, things get interesting if we add the array itself as an element of the array, which we do in the next example—in which I also sneaked in an reference operator as otherwise PHP would create a copy here:

$a = array( 'one' );
$a[] =& $a;
xdebug_debug_zval( 'a' );

which displays (after formatting):

a: (refcount=2, is_ref=1)=array (
        0 => (refcount=1, is_ref=0)='one',
        1 => (refcount=2, is_ref=1)=...
)

Graphically, it looks like:










You can see that the array variable ("a") as well as the second element ("1") now point to a variable container that has a "refcount" of "2". The "..." in the display above shows that there is recursion involved, which of course in this case it means that the "..." points back to the original array.

Just like before, unsetting a variable removes the symbol, and the reference count of the variable container it points to is decreased by one. So if we unset variable $a after running the above code, the reference count of the variable container that $a and element "1" point to gets decreased by one, from "2" to "1". This can be represented like:

(refcount=1, is_ref=1)=array (
        0 => (refcount=1, is_ref=0)='one',
        1 => (refcount=1, is_ref=1)=...
)

Graphically, it looks like:











Although there is no symbol in any scope pointing to this structure anymore, it can not be cleaned up either because the array element "1" still points to this same array. Because there is no external symbol pointing to it, there is no way for a user to clean up this structure anymore, and thus you get a memory leak. Fortunately, PHP will clean up this data structure at the end of the request, but before then this is taking up valuable space in memory. The mentioned situation happens often if you're implementing parsing algorithms or other things where you have a child point back at a "parent" element. The same situation can also happen with objects of course, where it actually happens easier as objects are always implicitly used by reference.

This might not be a problem if this only happens once or twice, but if there is thousands, or even millions of these memory losses, this obviously starts being a problem. Especially in long running scripts, such as daemons where the request basically never ends, or in large sets of unit tests. The latter caused problems for us while running the unit tests for the Template component of the eZ Components library. In some cases it would require over 2 GiB of memory, which our test server didn't quite have.

With that we conclude this introduction, for more information on how PHP deals with variables I can point you at June 2005 issue of php|architect. That article is also available on-line as PDF (http://derickrethans.nl/files/phparch-php-variables-article.pdf). In the next installment, we're going to discuss the solution to the memory leak problem with circular references.

Saturday, May 26, 2012

tcpdf svg php


require_once('../tcpdf.php');

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->AddPage();
$pdf->ImageSVG($file='file.svg', $x=15, $y=30, $w='', $h='', $link='', $align='', $palign='', $border=0, $fitonpage=false);
$pdf->Output('sample.pdf', 'F');

Monday, December 19, 2011

PHP OOP Simple Factory



The Simple Factory isn't an actual pattern, but a way of doing things. This will help you see how we can apply a real Factory pattern in the upcoming tutorials. All factories have to do with object creation by delegating the responsibilities to the appropriate classes.. In OOP terms hopefully without scaring you away, we are "Encapsulating what varies", and "Leaving the object open for extension and closed for modification".
Source: http://jream.com/lab/open-source

Monday, October 3, 2011

disable php warning

php.ini file
display_errors = Off


or add this at the top the scrpt
error_reporting(0);

Sunday, September 4, 2011

run PHP code from a .html File

add these lines to your .htaccess file

for a particular file name test.html
AddType application/x-httpd-php .html

for all file .html
AddType application/x-httpd-php .html

for a particular file name test.htm
AddType application/x-httpd-php .htm

for all file .htm
AddType application/x-httpd-php .htm



Friday, August 26, 2011

CMS Comparison: Joomla vs. Drupal vs. WordPress



http://WISRjoomla.com - Discusses CMS Comparison: Joomla vs. Drupal vs. WordPress.

http://WISRjoomla.com is a Joomla tutorial website that provides Beginner to Advanced Joomla Video Tutorials, Joomla Support and an active Joomla Community. See other free videos that provides a brief overview and introduction to the services that are available at http://WISRjoomla.com

Joomla is the leading Open Source Content Management System (CMS) for publishing content on the Internet. Joomla is often compared to Drupal and WordPress content management systems and is perfect for beginners and small to medium size businesses.

Tuesday, May 17, 2011

PHP Tutorial #1 - Covering the basics


This PHP tutorial covers the echo, the if...else statement and integer variables. A more in depth written tutorial shall be available on my website soon. http://www.rascal999.co.uk/

Sunday, May 1, 2011

NetBeans for PHP


the new NetBeans 7.0 (seven point O) is superb... dont believe just check it out. I was struggling with Adobe's Dreamweaver CS5 and Eclipse with my new php project.. needed version control.. debugging, automatic coding, intelligent interpreter which understands as well write code... just kidding..  yea yea i did spent the entire day configuring SVN with eclipse (but i failed with DW CS5). NetBeans is very easy.. its almost Microsoft Visual Studio its freeee.. totally totally free... dont forget check out the pluggin page as well. Oops dont forget to check the webcast page as well... superb tool or IDE... the download is very small if you wanna download the entire thing it would be around 250MB, but you choose the flavour you want... 

Download the latest NetBeans 7.0 from http://netbeans.org/
















choose the IDE Language and the platform (Windows, Linux(x86/x64), Solaris(x86/x64), Mac OS X, OS Independent Zip --didnt try this option)

please double click the NetBeans IDE 7.0 and enjoy coding or just looking at it may be :))



Thursday, April 14, 2011

Appserv Open Project

AppServ is a very easy to use all in one (Apache, PHP, MySql) bundle and phpMyAdmin

yii framework



The FastSecure and ProfessionalPHP Framework

Yii is a high-performance PHP framework best for developing Web 2.0 applications.

Yii comes with rich features: MVC, DAO/ActiveRecord, I18N/L10N, caching, authentication and role-based access control, scaffolding, testing, etc. It can reduce your development time significantly.




Yii Tour - 1st Stop: Preparation Station from jeff winesett on Vimeo.


Yii Tour - 2nd Stop - Saying Hello to World from jeff winesett on Vimeo.


Yii Tour - 3rd Stop - CRUD County from jeff winesett on Vimeo.


Yii Tour - 4th Stop - Down To The Database (and back again) from jeff winesett on Vimeo.

About

Blogger templates