30th Dec2014

PHP Perfection: Ajax Image Upload Script

by Gyro

Today I stumbled upon a neat little PHP image upload script that includes the html/js front-end script to easily upload images. The script displays a simple upload form, you select an image from your computer, click on upload, and the script uploads the image via php. Once the image has been uploaded, a thumbnail will be displayed in place of the upload form and button. The script can be easily implemented into any existing code and customization is also no problem for anyone with some html/js/php expience.

For me, it's simply perfect :)

 

567

08th Aug2014

Best way to manipulate images using PHP?

by Gyro

I have deployed lots of different solutions to manipulate images with PHP, but today I stumbled upon probably the best PHP library out there.

It is called WideImage (requires PHP 5.2+ with GD2 extension), and it makes resizing, cropping, rotating, adding watermarks. and much more super easy.

Check out WideImage, I'm sure you'll love it!

Enjoy! :crazy:

749

21st Feb2013

Restore large mysql databases with nginx 504 Gateway Timeout

by Gyro

I had to restore a 36MB sql database today and kept getting a "504 Gateway Time-out".

After playing around with the config files, adding fastcgi_read_timeout to the settings for php5-fpm, and fiddling with the php.ini, I realized that this can be accomplished with one single line using ssh:

Assuming that you have packed the .sql file into a zip file, and you are currently in that folder:
# gunzip < databasebackup.sql.gz | mysql -DDATABASENAME -uUSERNAME -pPASSWORD

DATABASENAME = name of the database
USERNAME = mysql username
PASSWORD = mysql password

I hope this helps when trying to restore large mysql databases with nginx 504 Gateway Timeout

2338

15th Jan2013

Install php-gd on Ubuntu without recompiling php

by Gyro

For some reason one of my client's website stopped showing captcha codes for the mad4joomla component, the server is running nginx with php-fpm instead of Apache.

Looking at the error.log, I got

 [error] 21110#0: *100 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Call to undefined function imageantialias() in /var/www/clients/client1/web1/web/components/com_mad4joomla/sec/im4.php on line 39"

Time to check the server, that function is supposed to be part of the GD library.

First, I checked if GD was loaded:
# php5 -m | grep -i gd

gd

ok, so GD is there… now on to the function.

# php -r "var_dump( function_exists(‘imageantialias'));"

bool(false)

hmm… :wooty:

So, on I went to google, trying to find out how I can get imageantialias() to work (again)… to my surprise I actually stumbled upon a post talking about the exact same issue, while using the same server setup -- Ubuntu with ISP Config. The solution presented was to recompile php, which I was reluctant to do.

After some more searching I came across this amazing little page (http://nossie.addicts.nl/php5-gd.html) which I am going to post (slightly modified) right here, mainly because it looks like that page could be gone tomorrow…

Getting bundled php-gd working on Ubuntu without having to recompile php

Like many other people out there, I ran into the problem that the version of php-gd shipped with Ubuntu (and Debian) is different from the version used by many other distributions.
The version Ubuntu uses misses some functions like imagerotate and imageantialias, which are needed by an increasing number of software projects.

One solution to this problem is recompiling PHP with the bundled version of GD.
It is not particularly hard to do, but there are a reasons not to do it, one of them being that it is not neccesary.

The following steps solved the problem for me.

Install what is needed to do this:
# sudo apt-get install php5-cli php5-gd rpm mc

Check what version of php you are running (php5 -v). In my case it was 5.3.2-1ubuntu4.7
Go to rpmfind.net and search for php-gd-bundled
Download the version that matches your PHP version and architecture, php-gd-bundled-5.3.2-1mdv2010.1.x86_64.rpm in my case.

If you have installed mc (midnight commander) and rpm, you can use mc to open the downloaded .rpm file (start mc, goto the .rpm file and hit enter)
Inside the .rpm file you will see CONTENT.cpio, navigate to that and hit enter. Goto usr/lib64/php/extensions, there you will find gd-bundled.so.

The original php5-gd is installed at /usr/lib/php5/20090626/gd.so (for my installation), backup the original gd.so, and copy the gd-bundled.so to that location and rename to gd.so (you can copy files with F5 in mc).
The new gd.so expects to find libjpeg.so.8, this was not present on my system, but that can be sovled by creating a symlink from the installed libjpeg.so
On my system I found /usr/lib/libjpeg.so.64.0.0
Create the symlink with:
# ln -s /usr/lib/libjpeg.so.62.0.0 /usr/lib/libjpeg.so.8

This is it, restart apache and you should be running with the bundled version of gd.
# php -r "var_dump(function_exists(‘imageantialias'));"
Should return bool(true) this time.

Now, if there is an update of the php5-gd package, your modified version gets overwritten. To prevent this from happening, you can hold the php5-gd package so it will not be updated.
# aptitude hold php5-gd

I hope this will help you as much as it did me!

Enjoy :enjoy:

51624

15th Dec2012

WordPress 3.5 – PHP Warning: Missing argument 2 for wpdb::prepare()

by Gyro

I just updated a work in progress to WordPress 3.5, and ended up with broken permalinks (to fix that: reset to default permalinks, then set the custom permalinks again), and my favorite plugin "Network Shared Posts" gave a super-long error message on each page, something along the lines of
PHP Warning: Missing argument 2 for wpdb::prepare()…blablabla…

So, after doing some research I found out that the root of all evil are the WordPress developers! Or to be more precise, they decided to make their functions more strict, resulting in a lot of plugins now showing the error "PHP Warning: Missing argument 2 for wpdb::prepare()", after someone updates to WordPress 3.5 and before the author has the chance to update, or in some cases totally rewrite their plugin. Read this article for more on this.

Here is my work-around to literally hide all "PHP Warning: Missing argument 2 for wpdb::prepare()" error messages:

(more…)

1348