upload obrazku - zmensenie

Programovacie jazyky, rady, poradňa...
geoge046
Star
Star
Používateľov profilový obrázok
Príspevky: 608
Registrovaný: 10 aug 2005, 14:15
Bydlisko: Prievidza
Kontaktovať používateľa:

upload obrazku - zmensenie

Príspevok od používateľa geoge046 »

Cafte,
po krátkom odmlcani som tu zas :p a mam jeden problemiq

potreboval by som vytvorit nieco taketo:
pred uploadom skontroluje velkost obrazku, ci je mensia ako 500 x 800, ak je mensi obrazok, normalne ho upne, ale ak je vacsi, tak ho pomocou ImageCopyResize zmensi na sirku 500px a potom vysku by uz len prisposobilo zmenseniu sirky, a na koniec by ho uz len uplo do DB

Vedeli by ste nieco take vytvorit? dík :)

Začal som takto:

Kód: Vybrať všetko

list($width, $height, $type, $attr) = GetImageSize($obrazok);
if($width < 500 && $height < 800) {
//upload
} else {
a tu to uz skrype :(
IgiPoP
Medium Star
Medium Star
Používateľov profilový obrázok
Príspevky: 446
Registrovaný: 12 sep 2005, 8:17
Bydlisko: MT

Príspevok od používateľa IgiPoP »

uz som to tu davnejsie posielal, ale nemozem to najst, pravdepodobne to bolo premazane.

Kód: Vybrať všetko

$typ=$_FILES['imagefile']['type'];
if (($typ == "image/pjpeg") or ($typ == "image/jpeg"))
{
  $thumbsize=500; // stvorcova velkost
  $dir="img/upload" // cesta ukladania na serveri
  $imagefile_name = $_FILES['imagefile']['name'];
  $imgfile = $_FILES['imagefile']['tmp_name'];
  list($width, $height) = getimagesize($imgfile);
  $imgratio=$width/$height;
  if (($width>$thumbsize) or ($height>$thumbsize))
  {
    if ($imgratio>1)
    {
      $thumbwidth = $thumbsize;
      $thumbheight = $thumbsize/$imgratio;
    }
    elseif ($imgratio==1)
    {
      $thumbheight = $thumbsize;
      $thumbwidth = $thumbsize;
    }
    else
    {
      $thumbheight = $thumbsize;
      $thumbwidth = $thumbsize*$imgratio;
    }
    $thumbimg = ImageCreateTrueColor($thumbwidth,$thumbheight);
    $thumbsource = imagecreatefromjpeg($imgfile);
    imagecopyresized($thumbimg, $thumbsource, 0, 0, 0, 0, $thumbwidth, $thumbheight, $width, $height);
    imagejpeg($thumbimg,"$dir/$imagefile_name",100);
  }
  else 
  { 
    move_uploaded_file ($_FILES['imagefile']['tmp_name'], "$dir/".$_FILES['imagefile']['name']) or die ("Could not copy"); 
  }
} 
else  
  echo "Je mi luto ale zožujem iba JPEGeèko a to do 2ch Mega!";
preedituj si to na svoj obraz :wink:
good luck.
geoge046
Star
Star
Používateľov profilový obrázok
Príspevky: 608
Registrovaný: 10 aug 2005, 14:15
Bydlisko: Prievidza
Kontaktovať používateľa:

Príspevok od používateľa geoge046 »

A potom keby to chcem prerobit na ukladanie do DB? tak to ako?
alebo staci povedat, ako ten vysledny obrazok prevediem do premennej (a v tej premennej bude ulozeny v binarnom štýle) ;)
geoge046
Star
Star
Používateľov profilový obrázok
Príspevky: 608
Registrovaný: 10 aug 2005, 14:15
Bydlisko: Prievidza
Kontaktovať používateľa:

Príspevok od používateľa geoge046 »

Takze takto som si nakoniec cely ten script upravil :)

aa ked dam obrazok vacsi nez je povolene tak mi vypise toto:

Kód: Vybrať všetko

Warning: imagejpeg(): supplied argument is not a valid Image resource in /www/***/public_html/pridaj/admin.php on line 46

Warning: fread(): supplied argument is not a valid stream resource in /www/***/public_html/pridaj/admin.php on line 47

Warning: fclose(): supplied argument is not a valid stream resource in /www/***/public_html/pridaj/admin.php on line 48

Kód: Vybrať všetko

#nastavenia
$max_velkost = 2048;
$max_width = "500";
$max_height = "800";
$allowed_types = array("image/bmp","image/gif","image/pjpeg","image/jpeg","image/png");

if(isset($action)) {
	#obrazky
	$fp = fopen($obrazok1, "r");
	$data = addslashes(fread($fp, filesize($obrazok1)));
	fclose($fp);
	$type = $obrazok1_type;
	$imgfile = $obrazok1;
	
	$fp2 = fopen($obrazok2, "r");
	$data2 = addslashes(fread($fp2, filesize($obrazok2)));
	fclose($fp2);
	$type2 = $obrazok2_type;
	$imgfile2 = $obrazok2;
	
	list($width, $height) = GetImageSize($obrazok1);
	list($width2, $height2) = GetImageSize($obrazok2);
	
	#obrazok1
if($width < $max_width) {
	$obrazok1final = $data;
} else {
	#definovane zmensenie obrazku
	$thumb_width = $max_width; 
	$thumb_height = $height - ($width - $max_width);
	
	#image obrazku
	$thumbimg = ImageCreateTrueColor($thumb_width,$thumb_height);
	if($obrazok1_type == "image/png") { $thumbsource = imagecreatefrompng($imgfile); 
	} elseif($obrazok1_type == "image/gif") { $thumbsource = imagecreatefromgif($imgfile); 
	} elseif($obrazok1_type == "image/pjpeg" || $obrazok1_type == "image/jpeg") { $thumbsource = imagecreatefromjpeg($imgfile); 
	} elseif($obrazok1_type == "image/bmp") { $thumbsource = imagecreatefromwbmp($imgfile); }
imagecopyresized($thumbimg, $thumbsource, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); 
if(obrazok1_type == "image/png") { $fp3 = fopen(imagepng($thumbing), "r");  
	$obrazok1final = addslashes(fread($fp3, filesize($thumbing)));
	fclose($fp3);
	} elseif($obrazok1_type == "image/gif") { $fp3 = fopen(imagegif($thumbing), "r");  
	$obrazok1final = addslashes(fread($fp3, filesize($thumbing)));
	fclose($fp3); 
	} elseif($obrazok1_type == "image/pjpeg" || $obrazok1_type == "image/jpeg") { $fp4 = fopen(imagejpeg($thumbing), "r");  
	$obrazok1final = addslashes(fread($fp3, filesize($thumbing)));
	fclose($fp3); 
	} elseif($obrazok1_type == "image/bmp") { $fp3 = fopen(imagewbmp($thumbing), "r");  
	$obrazok1final = addslashes(fread($fp3, filesize($thumbing)));
	fclose($fp3); }

#obrazok2
if($width2 < $max_width) {
	$obrazok2final = $data2;
} else {
	#definovane zmensenie obrazku
	$thumb_width2 = $max_width; 
	$thumb_height2 = $height2 - ($width2 - $max_width);
	
	#image obrazku
	$thumbimg2 = ImageCreateTrueColor($thumb_width2,$thumb_height2);
	if(obrazok2_type == "image/png") { $thumbsource2 = imagecreatefrompng($imgfile2); 
	} elseif($obrazok2_type == "image/gif") { $thumbsource2 = imagecreatefromgif($imgfile2); 
	} elseif($obrazok2_type == "image/pjpeg" || $obrazok2_type == "image/jpeg") { $thumbsource2 = imagecreatefromjpeg($imgfile2); 
	} elseif($obrazok2_type == "image/bmp") { $thumbsource2 = imagecreatefromwbmp($imgfile2); }
imagecopyresized($thumbimg2, $thumbsource2, 0, 0, 0, 0, $thumb_width2, $thumb_height2, $width2, $height2);
if(obrazok2_type == "image/png") { $fp4 = fopen(imagepng($thumbing2), "r");  
	$obrazok2final = addslashes(fread($fp4, filesize($thumbing2)));
	fclose($fp4);
	} elseif($obrazok2_type == "image/gif") { $fp4 = fopen(imagegif($thumbing2), "r");  
	$obrazok2final = addslashes(fread($fp4, filesize($thumbing2)));
	fclose($fp4); 
	} elseif($obrazok2_type == "image/pjpeg" || $obrazok2_type == "image/jpeg") { $fp4 = fopen(imagejpeg($thumbing2), "r");  
	$obrazok2final = addslashes(fread($fp4, filesize($thumbing2)));
	fclose($fp4); 
	} elseif($obrazok2_type == "image/bmp") { $fp4 = fopen(imagewbmp($thumbing2), "r");  
	$obrazok2final = addslashes(fread($fp4, filesize($thumbing2)));
	fclose($fp4); }
}}}
Aka je tam chyba? :cry:
IgiPoP
Medium Star
Medium Star
Používateľov profilový obrázok
Príspevky: 446
Registrovaný: 12 sep 2005, 8:17
Bydlisko: MT

Príspevok od používateľa IgiPoP »

mas tam chybu v nazve premennej. miesto "$thumbing" ma byt "$thumbimg".
fopen(imagejpeg($thumbing)
geoge046
Star
Star
Používateľov profilový obrázok
Príspevky: 608
Registrovaný: 10 aug 2005, 14:15
Bydlisko: Prievidza
Kontaktovať používateľa:

Príspevok od používateľa geoge046 »

A ešte nad čím sa tak zamýšľam je pridanie na určité miesto určitý obrázok, nejak tak ako to má aj STAHUJ.CZ. Ako by sa to dalo vyriešiť?

A ešte v tomto kóde čo sem dal IgiPop tak tam keď dam nejaky obrazok ktory ma niečo priesvitné tak po zmenšení to dá čierne (to býwale priesvitné). Dalo by sa to naprogramovať že by to aj po zmenšení bolo priesvitné alebo aspoň nie čierne ale biele? :)

Ďakujem =)
Napísať odpoveď