Avatar billede hoejgaard Juniormester
27. juni 2005 - 15:34 Der er 1 kommentar og
1 løsning

udsnit ikke midt af billede

Hej.
Jeg har et script som skulle tage et kvadratisk udsnit fra midten af et billede. Det ser ud som om at den tager udsnittet i midten mht horisontal retning men ikke i vertikalretning. Nogen der kan se hvad fejlen er ???

Her er scriptet :

<?php
$filename = "/customers/sti.dk/sti.dk/httpd.www/test/".$_GET['filnavn'];
$info = getimagesize( $filename );
$percent = 0.5; // if you want to scale down first
$imagethumbsize = 50; // thumbnail size (area cropped in middle of image)
switch ( $info[2] )
{
  case 1:
    // gif
   
// Content type
header('Content-type: image/gif');

// Get new dimensions
list($width, $height) = getimagesize($filename);
if ($width<100 or $height<100) { 
$new_width = $width ;
$new_height = $height ; }
else {

$new_width = $width * $percent;
$new_height = $height * $percent;  }
// Resample
$image_p = imagecreate($imagethumbsize , $imagethumbsize);  // true color for best quality
$image = imagecreatefromgif($filename);


// basically take this line and put in your versin the -($new_width/2) + ($imagethumbsize/2) & -($new_height/2) + ($imagethumbsize/2) for
// the 2/3 position in the 3 and 4 place for imagecopyresampled
// -($new_width/2) + ($imagethumbsize/2)
// AND
// -($new_height/2) + ($imagethumbsize/2)
// are the trick
imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);
break;

  case 2:
    // jpeg

// Content type
header('Content-type: image/jpg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
if ($width<100 or $height<100) { 
$new_width = $width* $percent;
$new_height = $height * $percent; }
else {

$new_width = $width * $percent;
$new_height = $height * $percent;  }
// Resample
$image_p = imagecreatetruecolor($imagethumbsize , $imagethumbsize);  // true color for best quality
$image = imagecreatefromjpeg($filename);


// basically take this line and put in your versin the -($new_width/2) + ($imagethumbsize/2) & -($new_height/2) + ($imagethumbsize/2) for
// the 2/3 position in the 3 and 4 place for imagecopyresampled
// -($new_width/2) + ($imagethumbsize/2)
// AND
// -($new_height/2) + ($imagethumbsize/2)
// are the trick
imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);



    break;
  default:
    die( 'Ikke understøttet. Vi tager kun imod jpg og gif!' );
}

switch ( $info[2] )
{
  case 1:
    imagegif($image_p);
    break;
  case 2:
    imagejpeg($image_p, null, 100);
    break;
}



?>
Avatar billede hoejgaard Juniormester
27. juni 2005 - 15:37 #1
scriptet har jeg hentet her :
http://dk2.php.net/manual/da/function.imagecopyresampled.php#52542

del at kartoon dot net
05-May-2005 10:38
This snippet allows you to grab a thumbnail from the center of a large image.  This was used for a client photo gallery for art to give a teaser of the image to come (only a small portion).  You could get dynamic with this value.  I also put in a scaling factor in case you want to scale down first before chopping.

<?php
// The file
$filename = 'moon.jpg';
$percent = 1.0; // if you want to scale down first
$imagethumbsize = 200; // thumbnail size (area cropped in middle of image)
// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($imagethumbsize , $imagethumbsize);  // true color for best quality
$image = imagecreatefromjpeg($filename);

// basically take this line and put in your versin the -($new_width/2) + ($imagethumbsize/2) & -($new_height/2) + ($imagethumbsize/2) for
// the 2/3 position in the 3 and 4 place for imagecopyresampled
// -($new_width/2) + ($imagethumbsize/2)
// AND
// -($new_height/2) + ($imagethumbsize/2)
// are the trick
imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);

// Output

imagejpeg($image_p, null, 100);
?>
Avatar billede hoejgaard Juniormester
27. juni 2005 - 16:58 #2
nå har fundet fejlen :

imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);

skulle være

imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_height , $width, $height);
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester