PHP Expert Programmer

Just another WordPress.com weblog

Archive for March, 2009

Fatal error: Allowed memory size of 8388608 bytes exhausted

Posted by sarapeter on March 26, 2009

Php Fatal error: Allowed memory size of 8388608 bytes exhausted

This error message can spring up in a previously functional PHP script when the memory requirements exceed the default 8MB limit. Don’t fret, though, because this is an easy problem to overcome.

To change the memory limit for one specific script by including a line such as this at the top of the script:

ini_set("memory_limit","12M");

The 12M sets the limit to 12 megabytes (12582912 bytes). If this doesn’t work, keep increasing the memory limit until your script fits or your server squeals for mercy.

You can also make this change permanently for all PHP scripts running on the server by adding a line like this to the server’s php.ini file:

memory_limit = 12M

Keep in mind that a huge memory limit is a poor substitute for good coding. A poorly written script may inefficiently squander memory which can cause severe problems for frequently executed scripts. However, some applications are run infrequently and require lots of memory like importing and processing a big data file.

Posted in Php Codes, Php Errors, Php Functions, Php Methods, Php Security | Tagged: , , , , | Leave a Comment »

Resize an image on the fly & keep its aspect ratio

Posted by sarapeter on March 25, 2009

Resize an image on the fly & keep its aspect ratio

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.

In other words, imagecopyresampled() will take an rectangular area from src_image of width src_w and height src_h at position (src_x ,src_y ) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x ,dst_y ).

If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if dst_image is the same as src_image ) but if the regions overlap the results will be unpredictable.

<?php
// The file
$filename 'test.jpg'
;
$percent 0.5
;

// Content type
header('Content-type: image/jpeg'
);

// Get new dimensions
list($width$height) = getimagesize($filename
);
$new_width $width $percent
;
$new_height $height $percent
;

// Resample
$image_p imagecreatetruecolor($new_width$new_height
);
$image imagecreatefromjpeg($filename
);
imagecopyresampled($image_p$image0000$new_width$new_height$width$height
);

// Output
imagejpeg($image_pnull100
);
?>

Posted in Php Codes, Php Functions, Php Methods | Tagged: , , , , | Leave a Comment »

Php extract method

Posted by sarapeter on March 1, 2009

“extract()” Method


Experimentally I found that calling extract() also shows the number of keys if the key is set and is not numeric ! Maybe there was a better definition than mine  . Please have a look to this scripts :
<?PHP
$var
["i"] = “a”
;
$var["j"] = “b”
;
$var["k"] = 1
;
echo
extract($var);
// returns 3
?>

<?PHP
$var2
["i"] = “a”
;
$var2[2] = “b”
;
$var2[] = 1
;
echo
extract($var2);
// returns 1
?>

 

(Arash Moslehi)

Posted in Php Functions, Php Methods | Tagged: , | Leave a Comment »