Showing posts with label php script. Show all posts
Showing posts with label php script. Show all posts

Thursday, August 20, 2009

how to increase upload_max_filesize, post_max_size and and memory_limit without accessing php.ini

When I was making a script to upload files, I encountered problems like I cannot upload files greater than 2MB. When I checked upload_max_filesize, post_max_size, and memory_limit there value are very low. I have this result when I executed this script in php


<?php

function convertBytes( $value ){
if (is_numeric( $value )) {
return $value;
}else{
$value_length = strlen( $value );
$qty = substr( $value, 0, $value_length - 1 );
$unit = strtolower( substr( $value, $value_length - 1 ) );
switch ( $unit ) {
case 'k':
$qty *= 1024;
break;
case 'm':
$qty *= 1048576;
break;
case 'g':
$qty *= 1073741824;
break;
}
return $qty;
}
}
echo 'Maximum file size: '.convertBytes(ini_get( 'upload_max_filesize' ))
/1048576.'MB'.'
';
echo 'post_max_size: '.convertBytes(ini_get('post_max_size'))
/1048576 . 'MB'.'
';
echo 'memory_limit: '.convertBytes(ini_get('memory_limit' ))
/1048576 . 'MB'.'
';
?>

Output:

Maximum file size: 2MB
post_max_size: 8MB
memory_limit: 16MB




To increase your upload_max_filesize, post_max_size, and memory_limit you can manipulate it using an .htaccess file. The first thing you do is to create an .htaccess file or if you already have just include this script in your .htaccess.

php_value upload_max_filesize "32M"
php_value post_max_size "32M"
php_value memory_limit "128M"


Important: PHP modifications only apply to the parent directory and its children. If the .htaccess file were placed in /var/www/html, then the changes would apply to the document root and recursively to all directories within.

You can now again run the php script above in order to see the changes.

Thursday, August 13, 2009

Image resizer script in php

Hello Guyz, I want to share something on how to resize image using PHP.
Below is the script to resize an image. Hope it will help.
If you have questions, you can email me here alreymark

Instructions:

1. Name the script whatever you want be sure it is a php file.
2. based on the script below I have 3 directory naming thumbs1, thumbs2 and thumbs3. This directories will be the location of the resized image based on category. ($cat=1, 2, 3)
3. Choose an image, it should be in jpeg format and place it together with the script.

for example if you are in the root of your site with a directory "/test/", you should come up with this location.

http://your-site.com/test/
http://your-site.com/test/resizer.php
http://your-site.com/test/testing.jpeg
http://your-site.com/test/thumbs1/
http://your-site.com/test/thumbs2/
http://your-site.com/test/thumbs3/

4. This should done the work. :)

<?php
function retrieveFolder($cat){
switch($cat){
case 1: $myfolder="thumbs1";break;
case 2: $myfolder="thumbs2";break;
case 3: $myfolder="thumbs3";break;
}//end switch

return $myfolder;
}//end function

function retrieveDimension($cat,$w,$h){
$d=array();
switch($cat)
{
case 1://First thumb size
if($w>$h){
$d[0] = 208 ;
$d[1] = $h * 208 /$w;
}
if($h>$w){
$d[0] = $w * 230/$h;
$d[1] = 230;
}
break;

case 2://second thumb size
if($w>$h){
$d[0] = 96 ;
$d[1] = $h * 96 /$w;
}
if($h>$w){
$d[0] = $w *106/$h;
$d[1] = 106;
}
break;

case 3://3rd thumb size
if($w>$h){
$d[0] = 120;
$d[1] = $h * 120 /$w;
}
if($h>$w){
$d[0] = $w *127/$h;
$d[1] = 127;
}
break;
}

return $d;
}//end function

class ResizeImage{
function ResizeImage($filename,$image_cat){
$myfolder=retrieveFolder($image_cat);
if(getimagesize($filename)){
$final_filename=$myfolder."/".basename($filename);
echo "base ".$filename." final ".$final_filename."
";
list($width, $height) = getimagesize($filename);
$dimension=retrieveDimension($image_cat,$width,$height);
$new_width=$dimension[0];
$new_height=$dimension[1];
$image_p =imagecreatetruecolor($new_width, $new_height);
$image =imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p,$final_filename,72);
imagedestroy($image_p);
imagedestroy($image);
}//if(file_exists($filename)
$this->rawfile=$filename;
$this->picfile=$final_filename;
}//end Constructor
function getFile(){return $this->picfile;}
function destroy(){ unset($this);}
}//end Class

$image = new ResizeImage("testing.jpg", 1);
$image->destroy();
$image = new ResizeImage("testing.jpg", 2);
$image->destroy();
$image = new ResizeImage("testing.jpg", 3);
$image->destroy();
?>