Archive for the ‘php’ Category

PHP curly braces in variable

Today I’ve got a discussion at stackoverflow about curly braces around PHP variable. The sample we’ve discussed about looked like this:

  $query = "SELECT * FROM t WHERE smth LIKE '{$id}'";

In my opinion, it’s no sense for curly braces in this case. Imagine that you have an array or object and you need to access some array’s value or object’s variable. In this case, you will need curly brackets:

1
2
3
  $query = "SELECT * FROM t WHERE smth LIKE '%{$array['key']}_jpg'";
  // or
  $query = "SELECT * FROM t WHERE smth LIKE '%{$object->prop}_jpg'";

So, curly braces are needed to specify the begin and end of a variable.

Also, curly braces may be used for dynamic variables in PHP:

1
2
3
4
  $a = 'smi';
  $b = 'le';
  $smile = ':-)';
  echo ${$a.$b}; // will show :-)

Get image resolution in PHP

Here’s a code snippet in PHP of how to get image resolution. I mean image width and height.

There’re lots of approaches of doing that and I’ve tryed to find the short one.

1
2
  list($width, $height, $type, $attr) = getimagesize($path_to_image);
  // then you may use $width and $height variables

So, to get the image resolution you should write only one line of PHP code. Also, you can get other info about the image using the getimagesize function. $path_to_image is a path to the image, for example, “/tmp/sunset.jpg” or “C:\Temp\image.jpg”, and in your case, it’s a path to the image the resolution of which you want to get. You can find more help about this function in PHP documentation.

To use this function the GD library is required. You don’t need any other frameworks or third party libraries.

WideImage performance and memory issues

Hi! in the previous post Trying to resize a set of large images in PHP I wrote that WideImage takes a lot of memory to resize big images, it was around 140MB and WideImage was resizing my images during ~30 seconds.

I’ve been thinking about this performance and memory issues and decided to write some PHP code that do the same but without WideImage library. I’ve used these PHP functions getimagesize, imagecreatefromjpeg, imagecreatetruecolor, imagecopyresized, imagejpeg and imagedestroy.

To my surprise, my custom PHP code do the same during ~15 seconds and takes only 250KB of memory!!! So, I think there are some problems with performance and memory in WideImage library. For me, the custom code that works faster and takes very less memory is a great solution.

Trying to resize a set of large images in PHP

Well, my PHP script terminates if I try to resize a set of big images. I use WideImage to resize images. Each image in a set is weighted around 10M. When I start my PHP script it terminates halfway and only several images from a collection have been resized. So, maybe it’s some limitation.. I am stating to find something in php.ini that starts with words “max..”. Wow! I’ve found memory_limit it’s a maximum amount of memory a script may consume. The default value of this setting is 128M. I set the new value of 500M for test. OK! My script has resized all the images! The amout of memory that is allocated is 140M, so it’s greater then a default value of 128M.

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
; memory_limit = 128M
memory_limit = 256M

I also log a running time of my script and it’s 28 seconds. In the php.ini the default value of maximum execution time is 30 seconds. To be on the safe side, I increased it to 60 seconds.

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
; max_execution_time = 30
max_execution_time = 60

GD extension required

I’ve tryed to use some GD functions in php. Locally everything is ok, I use xampp on Windows. But when I moved my site to the server I get this error:

PHP Fatal error: Uncaught exception ‘WideImage_Exception’ with message ‘WideImage requires the GD extension, but it’s apparently not loaded…

In my php app the WideImage library is used and it utilizes GD library. So, there isn’t GD extension installed. Well, let’s install it:

apt-get update
apt-get upgrade --show-upgraded
apt-get install php5-gd

After installing php5 gd library the error have been gone and WideImage classes begin to work properly.

PHP has lots of libraries and modules. To list them you can use this command:

apt-cache search php5-

Then you will see:

php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
php5-gd - GD module for php5
php5-gmp - GMP module for php5
php5-ldap - LDAP module for php5
php5-mysql - MySQL module for php5
[...]

If you want to install a module run the command:

sudo apt-get install module-or-library-name

It’s possible to install several modules or libraries at once just separate them with a space.

New line to paragraph in PHP

Here’s an example of how to convert new line (LF – line feed, ‘\n’) or carriage return (CR, ‘\r’) followed by new line (CR+LF, ‘\r\n’) to
or

. In case of double or more breaks we’ll get a paragraph but in case of one break we’ll have
.

$text = 'Lorem ipsum\r\n\r\ndolor sit\r\namet.';
// double or more nl to 

$text = preg_replace('/(\r?\n){2,}/', '

'

, $text); // nl to
$text = preg_replace('/(\r?\n)+/', '
'
, $text); echo '

'

. $text . '';

output:

Lorem ipsum

dolor sit
amet.

How to execute additional SQL in After Execute family events in CCS

Hi! This article is about how to execute some addtitional SQL after the primary SQL in the CodeCharge Studio’s Record has been executed.

Very often I have to perform some additional manipulations with the database after the Record submission. In this article I want to share the approach I use to reach the desired result. If you use a different approach to reach the same result I’m waiting your comments! :)

Read the rest of this entry »