Visa ett inlägg
Oläst 2008-05-19, 10:47 #5
grinditwp grinditwp är inte uppkopplad
Flitig postare
 
Reg.datum: Mar 2008
Inlägg: 320
grinditwp grinditwp är inte uppkopplad
Flitig postare
 
Reg.datum: Mar 2008
Inlägg: 320
Jag har en funktion som fungerar fint som jag brukar använda mig av.
Vad den gör är att spara en thumbnail lokalt.

Funktionen kan användas till mycket mera oxå, men bör fungera fint till det du vill.

Kanske hjälper? Anyway, feel free.

Snabbt förklarat:

$src = Bilden du vill skapa thumbnail av.
$dst = Vart vill du spara bilden.
$dstx = Bredd
$dsty = Höjd
$quality = Kvalité, default 75. (0-100)
$text = Om du vill ha in ett textfält i bilden, default = ingentext.

Kod:
<?
function resizeImage($src, $dst, $dstx, $dsty, $quality = 75, $text = ''){

$allowedExtensions = 'jpg jpeg gif png';

$name = explode(".", $src);
$currentExtensions = $name[count($name)-1];
$extensions = explode(" ", $allowedExtensions);

for($i=0; count($extensions)>$i; $i=$i+1){
if($extensions[$i]==$currentExtensions)
{ $extensionOK=1; 
$fileExtension=$extensions[$i]; 
break; }
}

if($extensionOK){

$size = getImageSize($src);
$width = $size[0];
$height = $size[1];

if($width >= $dstx AND $height >= $dsty){

$proportion_X = $width / $dstx;
$proportion_Y = $height / $dsty;

if($proportion_X > $proportion_Y ){
$proportion = $proportion_Y;
}else{
$proportion = $proportion_X;
}
$target['width'] = $dstx * $proportion;
$target['height'] = $dsty * $proportion;

$original['diagonal_center'] = 
round(sqrt(($width*$width)+($height*$height))/2);
$target['diagonal_center'] = 
round(sqrt(($target['width']*$target['width'])+
($target['height']*$target['height']))/2);

$crop = round($original['diagonal_center'] - $target['diagonal_center']);

if($proportion_X < $proportion_Y ){
$target['x'] = 0;
$target['y'] = round((($height/2)*$crop)/$target['diagonal_center']);
}else{
$target['x'] = round((($width/2)*$crop)/$target['diagonal_center']);
$target['y'] = 0;
}

if($fileExtension == "jpg" OR $fileExtension=='jpeg'){ 
$from = imagecreatefromjpeg($src); 
}elseif ($fileExtension == "gif"){ 
$from = imagecreatefromgif($src); 
}elseif ($fileExtension == 'png'){
 $from = imagecreatefrompng($src);
}

$new = imagecreatetruecolor ($dstx,$dsty);

imagecopyresampled ($new, $from, 0, 0, $target['x'], 
$target['y'], $dstx, $dsty, $target['width'], $target['height']);

//ADD TEXT
if($text != ''){
$im = $new;
if(!$im)
{
 die("");
}
$yellow = imagecolorallocate($im, 255, 255, 0);
$black = imagecolorallocate($im, 0, 0, 0);
$width = imagesx($im);
$height = imagesy($im);
imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);
$font = 4;
$leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;
imagestring($im, $font, $leftTextPos, $height-18, $text, $yellow);
$new = $im;
}


 if($fileExtension == "jpg" OR $fileExtension == 'jpeg'){ 
imagejpeg($new, $dst, $quality); 
}elseif ($fileExtension == "gif"){ 
imagegif($new, $dst); 
}elseif ($fileExtension == 'png'){
imagepng($new, $dst);
}
}
}
}
?>
För att bestämma bildens storlek brukar jag köra det något dynamiska systemet:

Kod:
$setwidth = 120;
$setheight = 80;

list($width, $height, $type, $attr) = getimagesize('mapp/uppladdadfil.jpg');
  if($width > $setwidth){
  $pr = $setwidth/$width;
  $w = $pr * $width;
  $h = $pr * $height;
  } else {
  	$w = $width;
  	$h = $height;
  }
  if($h > $setheight){
  $pr = $setheight/$h;
  $w = $pr * $w;
  $h = $pr * $h;
  } else {
  	$w = $w;
  	$h = $h;
  }
resizeImage('mapp/uppladdadfil.jpg', 'mapp/nyfil.jpg', $w, $h, 100);
grinditwp är inte uppkopplad   Svara med citatSvara med citat