Dzięki znalazłem też coś takiego, ale mam jeden problem...
function set_thumb($file, $photos_dir=photos,$thumbs_dir=thumbs, $square_size=150, $quality=150) {
//check if thumb exists
//get image info
list
($width, $height, $type, $attr) = getimagesize($photos_dir."/".$file);
//set dimensions
if($width> $height) {
$width_t=$square_size;
//respect the ratio
$height_t=round($height/$width*$square_size); //set the offset
$off_y=ceil(($width_t-$height_t)/2
); $off_x=0;
} elseif($height> $width) {
$height_t=$square_size;
$width_t=round($width/$height*$square_size); $off_x=ceil(($height_t-$width_t)/2
); $off_y=0;
}
else {
$width_t=$height_t=$square_size;
$off_x=$off_y=0;
}
$thumb=imagecreatefromjpeg($photos_dir."/".$file);
$thumb_p = imagecreatetruecolor($square_size, $square_size);
//default background is black
$bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
imagefill ( $thumb_p, 0, 0, $bg );
imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
imagejpeg($thumb_p,$thumbs_dir."/".$file,$quality);
}
}
$file = 'kurs.jpg';
function set_thumb($file, $photos_dir=photos,$thumbs_dir=thumbs, $square_size=150, $quality=150) {
$photos_dir=photos,$thumbs_dir=thumbs
jeśli zrobię:
$photos_dir=$folder1,$thumbs_dir=$folder2
wyrzuca mi błąd więc pytanie jakie apostrofy zastosować, aby było dobrze? chcę mieć foldery w zmiennych
Ok mam wychodzi coś takiego:
<?php
function set_thumb($file, $photos_dir, $thumbs_dir, $square_size=150, $quality=150) {
//check if thumb exists
//get image info
list
($width, $height, $type, $attr) = getimagesize($photos_dir.'/'.$file);
//set dimensions
if($width> $height) {
$width_t=$square_size;
//respect the ratio
$height_t=round($height/$width*$square_size); //set the offset
$off_y=ceil(($width_t-$height_t)/2
); $off_x=0;
} elseif($height> $width) {
$height_t=$square_size;
$width_t=round($width/$height*$square_size); $off_x=ceil(($height_t-$width_t)/2
); $off_y=0;
}
else {
$width_t=$height_t=$square_size;
$off_x=$off_y=0;
}
$thumb=imagecreatefromjpeg($photos_dir.'/'.$file);
$thumb_p = imagecreatetruecolor($square_size, $square_size);
//default background is black
$bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
imagefill ( $thumb_p, 0, 0, $bg );
imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
imagejpeg($thumb_p,$thumbs_dir.'/'.$file,$quality);
}
}
$photos_dir = 'photos';
$thumbs_dir = 'thumbs';
$file = 'kurs.jpg';
echo set_thumb
($file, $photos_dir, $thumbs_dir); ?>
A odnośnie PHPthumb to generuje do kwadratu, ale ucina zdjęcia chyba nie ma tam takiego rozwiązania jak to. Zapewne kiedyś komuś się przyda.