Jump to content
php.lv forumi

Images


Mikijs

Recommended Posts

Nezinu vai shis ir jaraksta sheit bet nu tomer tur ieksa ir paris rindinjas ar sql ;) :D

 

Piedavaju Fully Finished PHP+SQL (IMAGE_AR_UDENSZIMI)

 

 

1. Vaidzees vienu tabulu

$result = dbquery("DROP TABLE IF EXISTS bilde");
$result = dbquery("CREATE TABLE bilde (
foto VARCHAR(100) NOT NULL DEFAULT ''
PRIMARY KEY (user_id)
) TYPE=MyISAM;");

 

2. Vajadzes failu profils.php

<?php
  ob_start();
$paraksts = "1";
echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
'http://www.w3.org/TR/html4/loose.dtd'>
<html>
<head>
<title>php.lv</title>
</head>
<html>";
// Ieladet var tikai jpg/png
$disp_width_max=150;
$disp_height_max=80;			  
$edgePadding=15;					
$quality=100;						   
$default_watermark='watermarks/watermark.png';  // noradi savu uzdenszimes atrasanas vietu

if(isset($_POST['process'])){
	if(isset($_FILES['watermarkee']) && $_FILES['watermarkee']['error']==0){
		if(!isset($_POST['save_as'])) $_POST['save_as']='jpeg';
		if(!isset($_POST['v_position'])) $_POST['v_position']='center';
		if(!isset($_POST['h_position'])) $_POST['h_position']='center';
		if(!isset($_POST['wm_size'])) $_POST['wm_size']='1';
		if(!isset($_POST['watermark'])) $_POST['']=$default_watermark;
		$size=getimagesize($_FILES['watermarkee']['tmp_name']);
		if($size[2]==2 || $size[2]==3){
			$original=$_FILES['watermarkee']['tmp_name'];
			$xtarget_name=date('YmdHis').'_'.
   $target_name=$xtarget.'['.date('YmdHis').']_'.
				preg_replace('`[^a-z0-9-_.]`i','',$_FILES['watermarkee']['name']);
			$target=dirname(__FILE__).'/'.$target_name;
			$watermark=dirname(__FILE__).'/watermarks/watermark.png';
			$wmTarget=$watermark.'.tmp';

$ievietot_foto = "user_avatar='".$target_name."'";

$result = dbquery("UPDATE ".$db_prefix."users SET $set_avatar WHERE user_id='".$userdata['user_id']."'");
$result = dbquery("SELECT * FROM ".$db_prefix."users WHERE user_id='".$userdata['user_id']."'");
if (dbrows($result) != 0) {
	$userdata = dbarray($result);
echo "<center><a href='profils.php?p=".$target_name."' target='_parent'>Ok</a></center>";
}

				$origInfo = getimagesize($original); 
			$origWidth = $origInfo[0]; 
			$origHeight = $origInfo[1]; 

			$waterMarkInfo = getimagesize($watermark);
			$waterMarkWidth = $waterMarkInfo[0];
			$waterMarkHeight = $waterMarkInfo[1];

			// watermark sizing info
			if($_POST['wm_size']=='larger'){
				$placementX=0;
				$placementY=0;
				$_POST['h_position']='right';
				$_POST['v_position']='bottom';
				$waterMarkDestWidth=$waterMarkWidth;
				$waterMarkDestHeight=$waterMarkHeight;
				if($waterMarkWidth > $origWidth*1.05 && $waterMarkHeight > $origHeight*1.05){
					$wdiff=$waterMarkDestWidth - $origWidth;
					$hdiff=$waterMarkDestHeight - $origHeight;
					if($wdiff > $hdiff){
						// the width has the largest difference - get percentage
						$sizer=($wdiff/$waterMarkDestWidth)-0.05;
					}else{
						$sizer=($hdiff/$waterMarkDestHeight)-0.05;
					}
					$waterMarkDestWidth-=$waterMarkDestWidth * $sizer;
					$waterMarkDestHeight-=$waterMarkDestHeight * $sizer;
				}else{
					$wdiff=$origWidth - $waterMarkDestWidth;
					$hdiff=$origHeight - $waterMarkDestHeight;
					if($wdiff > $hdiff){
						$sizer=($wdiff/$waterMarkDestWidth)+0.05;
					}else{
						$sizer=($hdiff/$waterMarkDestHeight)+0.05;
					}
					$waterMarkDestWidth+=$waterMarkDestWidth * $sizer;
					$waterMarkDestHeight+=$waterMarkDestHeight * $sizer;
				}
			}else{
				$waterMarkDestWidth=round($origWidth * floatval($_POST['wm_size']));
				$waterMarkDestHeight=round($origHeight * floatval($_POST['wm_size']));
				if($_POST['wm_size']==1){
					$waterMarkDestWidth-=2*$edgePadding;
					$waterMarkDestHeight-=2*$edgePadding;
				}
			}
			resize_png_image($watermark,$waterMarkDestWidth,$waterMarkDestHeight,$wmTarget);

			$wmInfo=getimagesize($wmTarget);
			$waterMarkDestWidth=$wmInfo[0];
			$waterMarkDestHeight=$wmInfo[1];
			$differenceX = $origWidth - $waterMarkDestWidth;
			$differenceY = $origHeight - $waterMarkDestHeight;

			switch($_POST['h_position']){
				case 'left':
					$placementX = $edgePadding;
					break;
				case 'center':
					$placementX =  round($differenceX / 2);
					break;
				case 'right':
					$placementX = $origWidth - $waterMarkDestWidth - $edgePadding;
					break;
			}

			switch($_POST['v_position']){
				case 'top':
					$placementY = $edgePadding;
					break;
				case 'center':
					$placementY =  round($differenceY / 2);
					break;
				case 'bottom':
					$placementY = $origHeight - $waterMarkDestHeight - $edgePadding;
					break;
			}

			if($size[2]==3)
				$resultImage = imagecreatefrompng($original);
			else
				$resultImage = imagecreatefromjpeg($original);
			imagealphablending($resultImage, TRUE);

			$finalWaterMarkImage = imagecreatefrompng($wmTarget);
			$finalWaterMarkWidth = imagesx($finalWaterMarkImage);
			$finalWaterMarkHeight = imagesy($finalWaterMarkImage);

			imagecopy($resultImage,
					  $finalWaterMarkImage,
					  $placementX,
					  $placementY,
					  0,
					  0,
					  $finalWaterMarkWidth,
					  $finalWaterMarkHeight
			);

			if($size[2]==3){
				imagealphablending($resultImage,FALSE);
				imagesavealpha($resultImage,TRUE);
				imagepng($resultImage,$target,$quality);
			}else{
				imagejpeg($resultImage,$target,$quality); 
			}

			imagedestroy($resultImage);
			imagedestroy($finalWaterMarkImage);

?>

<p align="center">
 <img src="<?php echo $target_name ?>?id=<?php echo md5(time()) ?>" alt="" />
</p>
<?php
			unlink($wmTarget);
		}else{
		}
	}else{
?>
  <div>
<h1>Foto</h1>
<p class="errorMsg">Nevaru augšupielādēt</p>
  </div>
<?php
	}
}else{
?>
<div align="center">
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Tavs Foto<br><font class="small2">Tikai JPG tipus</font>
<br>	   <input class="text" type="file" name="watermarkee" />
<?php
	$dir=dirname(__FILE__).'/watermarks';
	if (is_dir($dir)) {
		if ($dh = opendir($dir)) {
			$i=0;
			$watermarks=array();
			while (($file = readdir($dh)) !== FALSE) {
				if(!preg_match('`\.png$`',$file)) continue;
				$watermarks[]=$file;
			}
			closedir($dh);
			asort($watermarks);
			foreach($watermarks as $file){
				$size=getimagesize($dir.'/'.$file);
				if($size[0] > $disp_width_max && $disp_width_max){
					$reduction=$disp_width_max / $size[0];
					$size[0]=$size[0]*$reduction;
					$size[1]=$size[1]*$reduction;
					$size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);
					if($size[1] > $disp_height_max && $disp_height_max){
						$reduction=$disp_height_max / $size[1];
						$size[0]=$size[0]*$reduction;
						$size[1]=$size[1]*$reduction;
						$size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);
					}
				}
				if($size[1] > $disp_height_max && $disp_height_max){
					$reduction=$disp_height_max / $size[1];
					$size[0]=$size[0]*$reduction;
					$size[1]=$size[1]*$reduction;
					$size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);
				}



				$i++;
			}
			if($i==0){
				echo '	 <tr>',"\n",
					 '	  <td>',"\n",
					 '	   Nav nevienas udenzimes ko ievietot.',"\n",
					 '	  </td>',"\n",
					 '	 </tr>',"\n";
			}
		}
	}
?>

	<input type="hidden" name="h_position" value="left" />
	<input type="hidden" name="v_position" value="bottom" />
	<input type="hidden" name="wm_size" value=".5" /> 
<br>
	<div align="center"><input type="submit" name="process" class="button" value="Ielādēt" /></div>
</form></div>
<?php
}
$page_display=ob_get_clean();

function resize_png_image($img,$newWidth,$newHeight,$target){
$srcImage=imagecreatefrompng($img);
if($srcImage==''){
	return FALSE;
}
$srcWidth=imagesx($srcImage);
$srcHeight=imagesy($srcImage);
$percentage=(double)$newWidth/$srcWidth;
$destHeight=round($srcHeight*$percentage)+1;
$destWidth=round($srcWidth*$percentage)+1;
if($destHeight > $newHeight){
	$percentage=(double)$newHeight/$srcHeight;
	$destHeight=round($srcHeight*$percentage)+1;
	$destWidth=round($srcWidth*$percentage)+1;
}
$destImage=imagecreatetruecolor($destWidth-1,$destHeight-1);
if(!imagealphablending($destImage,FALSE)){
	return FALSE;
}
if(!imagesavealpha($destImage,TRUE)){
	return FALSE;
}
if(!imagecopyresampled($destImage,$srcImage,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight)){
	return FALSE;
}
if(!imagepng($destImage,$target)){
	return FALSE;
}
imagedestroy($destImage);
imagedestroy($srcImage);
return TRUE;
}

echo '<?xml version="1.0" encoding="iso-8859-1"?>',"\n";

echo $page_display;

closetable();
?>

 

Izmēģini - Lai izdodās! Man sanāca!

Link to comment
Share on other sites

×
×
  • Create New...