Пак помощ с уроците!

kanadeca

Registered
Може ли за този скрипт http://web-tourist.net/login/login/view.php?st=1206 да ми напишете форма за оразмеряване. Тоест да НЕ показва картинката като я качиш,но автоматично да я ресайзва. Например оригиналната е 1280x1280 и то да я направи на 500x500 . Help!
 
В момента на се показва картинката като я качиш.
Относно оразмеряването ... винаги ли ще са едни и същи height и width** и да се пази ли оригинала.

** ако ДА - кажи размери :)
 
Не ми остана време днеска да модвам кода.
Ето функцията, която прави thumb от картинки.

Код:
<?php

function thumbnail( $source_file, $destination_file, $new_width, $new_height)
{
   list($img_width,$img_height) = getimagesize($source_file); 
   
   $thumbnail = imagecreatetruecolor($new_width,$new_height); 

   if ( strpos($source_file,".gif") ) { $img_source = imagecreatefromgif($source_file); }
   if ( (strpos($source_file,".jpg")) || (strpos($source_file,".jpeg")) )
   { $img_source = imagecreatefromjpeg($source_file); }
   if ( strpos($source_file,".bmp") ) { $img_source = imagecreatefromwbmp($source_file); }
   if ( strpos($source_file,".png") ) { $img_source = imagecreatefrompng($source_file); }
 
   imagecopyresampled($thumbnail, $img_source, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
   imagePNG( $thumbnail, $destination_file, 100 );
 
   imagedestroy($img_source);
   imagedestroy($thumbnail);
}

?>


$new_width - новата широчина
$new_height - новата височина
$source_file - Адреса към ъплоудваната картинка
$destination_file - Новия адрес към картинката

ето как се ползва функцията... пример:
thumbnail( "".$url."", "./".$kartinka."", "55", "55");
Ако не успееш да го направиш, утре по някое време ще модна кода :wink:
 
Ами човек,нещо не можах да се справя. Ще съм ти наистина много благодарен да моднеш кода. Ако няма да е проблем можеш ли да погледнеш и тук защото тези неща наистина ми трябват пък си нямам и понятие как да станат. :wink:
 
Ето кода.
Само оразмеряването на bmp нещо не успях да го подкарам ... :roll:

Код:
<?php

function thumbnail( $source_file, $destination_file, $new_width, $new_height)
{
   list($img_width,$img_height) = getimagesize($source_file); // Get the original dimentions



$filename = $source_file;

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $new_width;
$newheight = $new_height;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$razshir = strtolower(strrchr($source_file, "."));

   if ( $razshir == ".gif" ) {
   $img_source = imagecreatefromgif($source_file);
   imagecopyresized($thumb, $img_source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
   imagegif($thumb, $destination_file);
   }
   
   
   if ( ($razshir == ".jpg") || ($razshir == ".jpeg" ) ){
   $img_source = imagecreatefromjpeg($source_file);
   imagecopyresized($thumb, $img_source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
   imagejpeg($thumb, $destination_file);
   }
   
  
   if ( $razshir == ".bmp" ) {
   $img_source = imagecreatefromjpeg($source_file);
   imagecopyresized($thumb, $img_source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
   image2wbmp($thumb, $destination_file);
   }
 
  
   if ( $razshir ==  ".png" ) { 
   $img_source = imagecreatefrompng($source_file);
   imagecopyresized($thumb, $img_source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
   imagepng($thumb, $destination_file);
   }
   
   



}



// Максимална големина на файла за качване (сега е 2 ГБ)
$MAX_SIZE = 268435456;


$FILE_EXTS = array('.jpg','.png', '.gif', '.bmp', 'jpeg');


// Възможност за триене на качени файлове. При TRUE - може да се трият, при FALSE - не.
$DELETABLE = false;



$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$upload_dir = "files/";
$upload_url = $url_dir."/files/";
$message ="";


if (!is_dir("files")) {
if (!mkdir($upload_dir))
die ("upload_files директорията не съществува и създаването и е невъзможно.");
if (!chmod($upload_dir,0755))
die ("change permission to 755 failed.");
}


if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
fclose($resource);

if (strpos($_REQUEST[del],"/.")>0);
else if (strpos($_REQUEST[del],$upload_dir) === false);
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
unlink($_REQUEST[del]);
print "<script>window.location.href='$url_this?message=deleted successfully'</script>";
}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resource);

$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));

// Ако променята макс. големина на качващия се файл променети и по-долното съобщение.
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "Този файл е над 2 GB.";

else if (!in_array($file_ext, $FILE_EXTS))
$message = "Извинете, $file_name($file_type) не може да се качи(файла не е поддържан).";
else
$message = do_upload($upload_dir, $upload_url);

print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Невалиден формат.";


$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir$file'>".$file."</a> - URL: <b>$upload_url$file</b>";
if ($DELETABLE)

$filelist .= " Качен на ".date("d-m H:i", filemtime($upload_dir.$file))
."";
$filelist .= " <a style='text-decoration:none; font-weight:bold' href='?del=$upload_dir".urlencode($file)."' title='delete'>x</a>";
$filelist .="<br>";
}
}

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;


if ( $file_name =="") {
$message = "Грешно име";
return $message;


}

$result = move_uploaded_file($temp_name, $file_path);
thumbnail( $file_path, $file_path."-thumb.png", "648", "648"); 
unlink($file_path);
rename($file_path."-thumb.png", $file_path);

if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
$message = ($result)?"$file_name е качен успешно." :
"Има грешка с избрания файл.";
return $message;
}

?>


<html>
<head>
<title>Форма за качване</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<link rel=stylesheet href=style.css>
</head>
<body>
<br><br>
<center>
<font color=red><?=$_REQUEST[message]?></font>
<br>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Качи файл <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">
</form>
<br><b><u>Качени файлове:</b></u><br><br>
По-долу е индексирането на файлове:<br />
<?=$filelist?><br>Този скрипт е направен от
<a style="text-decoration:none" href="http://deam0n.hit.bg">deam0n</a>
</sup></small>
</center>
 

Back
Горе