thumbnail на качена снимка

Lam40

Registered
Днес си написах едно скриптче за качване на снимки. Проблема е, че искам при качването да ми прави картинката с размери 100х100. Ако можете да ми помогнете, ще съм Ви много благодарен. Ето го и скрипта:

html
Код:
<form method="post" action="#" enctype="multipart/form-data">
	<div>
	<input type="file" name="fileimage" />
	<input type="submit" name="submit_up" value="Submit" />
	</div>
	</form>
php
Код:
$now = time() . '_' . $u_id;
$imagename = stripslashes($_FILES['fileimage']['name']);
$extension = getExtension($imagename);
$extension = strtolower($extension);
$newimagename = $now . '.' . $extension;
$newimagethumbname = $now . 't' . ".jpg";
$imagedir = "files/" . $newimagename;
$imagethumbdir = "files/" . $newimagethumbname;
if(preg_match('/\\.(jpg|JPG|gif|png)$/i', $imagename)){
@move_uploaded_file($_FILES['fileimage']["tmp_name"], $imagethumbdir);
}

*getExtensions е писана функция.

Благодаря! :lol:
 
Код:
function make_thumbnail($file,$dest2,$wi,$h){	
	list($width,$height,$type,$attr)=getimagesize($file);
  	switch($type) {
	    case 1:$in_jpg=imagecreatefromgif($file);break;
	    case 2:$in_jpg=imagecreatefromjpeg($file);break;
	    case 3:$in_jpg=imagecreatefrompng($file);break;
	    default: echo "Проблеми с формата на картинката"; return;
  	}
  	$w=$height/1.333333333;
  	$new_width=$width/($height/$h);			
  	if(($width / $height) >= 1.34){
  		$pos_total = ( $wi / 2 ) - (( $width / ( $height / $h )) / 2 );
  	}elseif((($width / $height) < 1.333) and (($width / $height) > 1) and ($h > 100) ){
  		$pos_total = ( $wi / 2 ) - (( $width / ( $height / $h)) / 2 );
  	}elseif((($width / $height) < 1.333) and (($width / $height) > 1) and ($h < 100)){
  		$pos_total = (( $wi / 2 ) - (( $width / ( $height / $h )) / 2 )) + 1;
  	}elseif(($width / $height) < 1){
  		$pos_total = ( $wi / 2 ) - (( $width / ( $height / $h )) / 2 );
  	}elseif(($width / $height) == 1){
  		$pos_total = ( $wi / 2 ) - (( $width / ( $height / $h )) / 2 );
  	}else{
  		$pos_total = 0;
  	}
  	$im=imagecreatetruecolor($wi,$h) or die("Проблеми с 'GD2'");
  	if($type == 2){
	  	for($y=0;$y<$h;$y++){
	  		for($x=0;$x<$wi;$x++){
	  			imagesetpixel($im,$x,$y,imagecolorallocate($im,255,255,255));
	  		}
	  	}
  	}else{
  		imagecolortransparent($im,'');
  	}
  	imagecopyresampled($im,$in_jpg,$pos_total,0,0,0,$new_width,$h,ImageSX($in_jpg),ImageSY($in_jpg));
  	switch($type){
  		case 1:imagegif($im,$dest2);break;
  		case 2:imagejpeg($im,$dest2,97);break;
  		case 3:imagepng($im,$dest2);break;
  	}
  	imagedestroy($in_jpg);
}

разгледай я, можеш да я пооптимизираш малко...
 
Нещо което ползвам...

Код:
function createThumb($source_file, $max_dimension, $path)
{
	$name_file = $source_file['name'];
	$source_file = $source_file['tmp_name'];
	list($img_width,$img_height) = getimagesize($source_file);
	$aspect_ratio = $img_width / $img_height;

	if ( ($img_width > $max_dimension) || ($img_height > $max_dimension) )
	{
		if ( $img_width > $img_height )
		{
			$new_width = $max_dimension;
			$new_height = $new_width / $aspect_ratio;
		}
		elseif ( $img_width < $img_height )
		{
			$new_height = $max_dimension;
			$new_width = $new_height * $aspect_ratio;
		}
		elseif ( $img_width == $img_height )
		{
			$new_width = $max_dimension;
			$new_height = $max_dimension;
		}
		else { echo "Error reading image."; return FALSE; }
	}
	else { $new_width = $img_width; $new_height = $img_height; }

	$new_width = intval($new_width);
	$new_height = intval($new_height);

	$thumbnail = imagecreatetruecolor($new_width,$new_height);

	if (strpos(strtolower($name_file),".gif"))
	{
		$img_source = imagecreatefromgif($source_file);
	}
	if ((strpos(strtolower($name_file),".jpg")) || (strpos(strtolower($source_file),".jpeg")))
	{
		$img_source = imagecreatefromjpeg($source_file);
	}
	if (strpos(strtolower($name_file),".bmp"))
	{
		$img_source = imagecreatefromwbmp($source_file);
	}
	if (strpos(strtolower($name_file),".png"))
	{
		$img_source = imagecreatefrompng($source_file);
	}

	Imagecopyresampled($thumbnail, $img_source, 0, 0, 0, 0, $new_width, $new_height,imagesx($img_source),imagesy($img_source));
	imagePNG($thumbnail, $path);

	imagedestroy($img_source);
	imagedestroy($thumbnail);
}

и после направо...

createThumb('kartinka.jpg', 130, 'thumb_kartinka.png');
 
Оттук оттам подбрах това нещо:
Код:
$source_pic = $imagethumbdir;
$destination_pic = 'files/destination.jpg';
$max_width = 100;
$max_height = 100;

$src = imagecreatefromjpeg($source_pic);
list($width,$height)=getimagesize($source_pic);

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if( ($width <= $max_width) && ($height <= $max_height) ){
    $tn_width = $width;
    $tn_height = $height;
    }elseif (($x_ratio * $height) < $max_height){
        $tn_height = ceil($x_ratio * $height);
        $tn_width = $max_width;
    }else{
        $tn_width = ceil($y_ratio * $width);
        $tn_height = $max_height;
}

$tmp=imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

imagejpeg($tmp,$destination_pic,100);
imagedestroy($src);
imagedestroy($tmp);

Благодаря и на двама Ви. :lol: Все пак ще пробвам и Вашите скриптове след малко. :wink:
 

Back
Горе