Wednesday, March 5, 2008

Resize image

<?php

$conn = mysql_connect("localhost", "username", "password") or die(mysql_error());

mysql_select_db('test', $conn) or die(mysql_error());

if(@$_FILES['userfile']['tmp_name']){

resize();

$q = "INSERT INTO test VALUES ('', '".$blob_arr['image']."', '".$blob_arr['thumb']."', '".$blob_arr['type']."')";

$aa = mysql_query($q,$conn) or die(mysql_error());


}


function resize(){

global $blob_arr;

$temp_name =$_FILES['userfile']['tmp_name'];

$userfile_name =$_FILES['userfile']['name'];

$userfile_size =$_FILES['userfile']['size'];

$userfile_type =$_FILES['userfile']['type'];


$thumb_width=90;

$thumb_height=90;

$image_width=300;

$image_height=400;


if (!($userfile_type =="image/pjpeg" OR $userfile_type =="image/jpeg" OR $userfile_type=="image/gif" OR $userfile_type=="image/png" OR $userfile_type=="image/x-png")){

die ("You can upload just images in .jpg .jpeg .gif and .png format!<br / >");

}

$data = fread(fopen($temp_name, "rb"), filesize($temp_name));

$src_image = imagecreatefromstring($data);

$width = imagesx($src_image);

$height = imagesy($src_image);

if ($thumb_width && ($width < $height)) {

$thumb_width = ($thumb_height / $height) * $width;

} else {

$thumb_height = ($thumb_width / $width) * $height;

}

if ($image_width && ($width < $height)) {

$image_width = ($image_height / $height) * $width;

} else {

$image_height = ($image_width / $width) * $height;

}

$dest_img = imagecreatetruecolor($thumb_width, $thumb_height);

$i_dest_img = imagecreatetruecolor($image_width, $image_height);

imagecopyresized($dest_img, $src_image,0, 0, 0, 0,$thumb_width, $thumb_height,$width, $height);

imagecopyresized($i_dest_img, $src_image,0, 0, 0, 0,$image_width, $image_height,$width, $height);

ob_start();

if($userfile_type == "image/jpeg" OR $userfile_type == "image/pjpeg"){

imagejpeg($dest_img);

}

if($userfile_type == "image/gif"){

imagegif($dest_img);

}

if($userfile_type == "image/png" OR $userfile_type == "image/x-png"){

imagepng($dest_img);

}

$binaryThumbnail = ob_get_contents();

ob_end_clean();

ob_start();

if($userfile_type == "image/jpeg" OR $userfile_type == "image/pjpeg"){

imagejpeg($i_dest_img);

}

if($userfile_type == "image/gif"){

imagegif($i_dest_img);

}

if($userfile_type == "image/png" OR $userfile_type == "image/x-png"){

imagepng($i_dest_img);

}

$binaryImage = ob_get_contents();

ob_end_clean();

if(!get_magic_quotes_gpc()){

$binaryThumbnail=addslashes($binaryThumbnail);

$binaryImage=addslashes($binaryImage);

}

$blob_arr['image']=$binaryImage;

$blob_arr['thumb']=$binaryThumbnail;

$blob_arr['type']=$userfile_type;

}

if(@$_POST['submit']){

$Rs = mysql_query("SELECT * FROM test WHERE id = '".mysql_insert_id()."'", $conn) or die(mysql_error());

$row_Rs = mysql_fetch_assoc($Rs);

header( "Content-type: ".$row_Rs['filetype']."");

echo $row_Rs['thumb'];

}else{

?>

<form name="form1" method="post" enctype="multipart/form-data" action="<? echo @$PHP_SELF; ?>">

<input id="userfile" name="userfile" type="file" />

<input type="submit" name="submit" value="Submit" />

</form>

<?

}

?>

No comments: