Citat:
Originally posted by twedmark@Dec 21 2004, 11:40
Tjena,
Använder en thumbnailcreator idag, men den kräver att PHP Safe Mode = OFF, vilket skapar problem.
Nån som har bra tips på en funktion att skapa thumbs i? Ska klara av att hålla samma aspektratio på bilderna, och kunna beskära thumsen så dom passar in i ett visst mått (tex 100x75pixl).
/J
|
Jag har mina bilder i MySQL men du kan säkert ändra så det går att använda vanliga bilder. Något som ser ut så här:
Kod:
<?php
# Constants
require_once "db.inc.php";
$phototable = $TABLEPREFIX . "photos";
if ( $_GET['thumb'] == "yes" ){
define(MAX_WIDTH, 100);
define(MAX_HEIGHT, 100);
} else {
define(MAX_WIDTH, 400);
define(MAX_HEIGHT, 400);
}
if($_GET['id']) {
db_conn();
$id = $_GET['id'];
$query = "select photo,type from $phototable where pid=$id";
$result = mysql_query($query) or die("Could not execute query " . mysql_error());
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$data = $row['photo'];
$type = $row['type'];
};
# Load image
$img = null;
$img = imagecreatefromstring($data);
# If an image was successfully loaded, test the image for size
if ($img){
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>
/Zoran