FAQ |
Kalender |
![]() |
#1 | |||
|
||||
Medlem
|
Hej!
bygger en nyhets funktion för våra lokala bollklubb där jag gärna hade viljat att bild-uploaden hade maxgräns dvs att bilden hade skalats ner till storleken 800 pix bredd eller dylikt?? Folk laddar nämligen upp bilder på flera megabyte som spränger utrymmet på nolltid. koden jag har just nu : <? include("variablar.php"); $datum = date("Y-m-d"); $tid = date("H:i:s"); $refid = md5(uniqid(rand(),1)); copy ($fil, "$uploadbilder/$fil_name"); $bilds = "$bildurl/$fil_name"; $sql = "INSERT INTO nyheter (undersida, sidnamn, sort, menytyp, rubrik, link, kat, bildnamn, bildurl, bildjustering, email, signatur, text, bildtext, datum, tid, ip) VALUES ('$undersida', '$sidnamn', '$sort', '$menytyp', '$rubrik', '$link', '$kat', '$fil_name', '$bilds', '$bildjustering', '$email', '$signatur', '$text', '$bildtext', '$datum', '$tid', '$REMOTE_ADDR')"; $pql = mysql_query($sql)or die(mysql_error()); header("location: $HTTP_REFERER"); ?> finns det enkel basic lösning på det? Tack på förhand.. |
|||
![]() |
![]() |
![]() |
#2 | |||
|
||||
Har WN som tidsfördriv
|
Om du har ImageMagick, något liknande (vet inte om man bör byta ut -thumbnail mot något annat när det upplösningen är högre än för thumbnails, men det är nog lätt att ta reda på):
Kod:
<? include("variablar.php"); $datum = date("Y-m-d"); $tid = date("H:i:s"); $refid = md5(uniqid(rand(),1)); exec('convert -quality 75 -thumbnail 800x600 ' . escapeshellarg(realpath($fil)) . ' ' . escapeshellarg($uploadbilder/$fil_name)); $bilds = "$bildurl/$fil_name"; $sql = "INSERT INTO nyheter (undersida, sidnamn, sort, menytyp, rubrik, link, kat, bildnamn, bildurl, bildjustering, email, signatur, text, bildtext, datum, tid, ip) VALUES ('$undersida', '$sidnamn', '$sort', '$menytyp', '$rubrik', '$link', '$kat', '$fil_name', '$bilds', '$bildjustering', '$email', '$signatur', '$text', '$bildtext', '$datum', '$tid', '$REMOTE_ADDR')"; $pql = mysql_query($sql)or die(mysql_error()); header("location: $HTTP_REFERER"); ?> |
|||
![]() |
![]() |
![]() |
#3 | ||
|
|||
Mycket flitig postare
|
Skillnaden på -thumbnail och -resize är att bildprofiler plockas bort när man använder -thumbnail. Så vill man ha kvar dem så är det bara att byta ut -thumbnail mot -resize.
|
||
![]() |
![]() |
![]() |
#4 | |||
|
||||
Mycket flitig postare
|
Refererar till http://www.bytemycode.com/snippets/snippet/624/ om du har tillgång till gd, snabbt enkelt och bra.
|
|||
![]() |
![]() |
![]() |
#5 | |||
|
||||
Medlem
|
Tack !
Jag testade det du skrev koala men det funkade inte, även om jag bytade till -resize enl. WizKid. (Warning: Division by zero) jag kollade länken gsoc: <code lang="Generic"> function resizeImage($file,$scale="",$width="",$height="") { // If they wish to scale the image. if (isset($scale)) { // Create our image object from the image. $fullImage = imagecreatefromjpeg($file); // Get the image size, used in calculations later. $fullSize = getimagesize($file); // If there is NOT a thumbnail for this image, make one. if (!file_exists("tn_".$file)) { // Create our thumbnail size, so we can resize to this, and save it. $tnImage = imagecreatetruecolor($fullSize[0]/$scale, $fullSize[1]/$scale); // Resize the image. imagecopyresampled($tnImage,$fullImage,0,0,0,0,$fu llSize[0]/$scale,$fullSize[1]/$scale,$fullSize[0],$fullSize[1]); // Create a new image thumbnail. imagejpeg($tnImage, "tn_".$file); // Clean Up. imagedestroy($fullImage); imagedestroy($tnImage); // Return our new image. return "tn_".$file; } // If there is a thumbnail file, lets just load it. else return "tn_".$file; } // If they want to force whatever size they want. elseif (isset($width) && isset($height)) { return "tn_".$file; } else { return false; } } </code> As im just looking over the code, it doesnt look like the force size works, i will revise this when i get a chance. han nämner att force size inte funkar,,? vad jag förstår så måste GD eller ImageMagick vara installerat på servern? :P |
|||
![]() |
![]() |
![]() |
#6 | |||
|
||||
Medlem
|
Kan man nu på något enkelt sätt sätta in en funktion i koden att "bilden är för stor", ha en maxgräns på 200 kb,?,,,, nu när jag inte har typ imagemagick tillgängligt.
|
|||
![]() |
![]() |
![]() |
#7 | ||
|
|||
Nykomling
|
Hej vidir,
Har du inte GD Library installerat då? Det ska tydligen följa med PHPs grundinstallation sedan 4.3.0 så det har du väl ändå? För att testa hur stora filerna som laddas upp kan du bara kontrollera att $_FILES['file']['size'] inte överskrider en viss storlek. Här är ett exempel: Kod:
$max_file_size = 10485760; if (isset($_POST['submit']) && $_FILES['image']['error'] == 0) { $file_size = $_FILES['image']['size']; if ($file_size > $max_file_size) { $error['filesize'] = 'Uploaded file size is more than 10MB'; } if (count($error) == 0) { /* kod för att ladda upp fil här */ } else { /* för stor fil, visa felmeddelande här */ } Kod:
function resizeImage($name, $target, $new_w, $new_h) { $system = explode(".", $name); if (preg_match("/jpg|jpeg/",$system[1])) { $src_img = imagecreatefromjpeg($name); } if (preg_match("/png/",$system[1])) {$src_img = imagecreatefrompng($name); } $old_x = imageSX($src_img); $old_y = imageSY($src_img); if ($old_x > $old_y) { $thumb_w = $new_w; $thumb_h = $old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w = $old_x*($new_w/$old_y); $thumb_h = $new_h; } if ($old_x == $old_y) { $thumb_w = $new_w; $thumb_h = $new_h; } $dst_img = ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if (preg_match("/png/",$system[1])) { imagepng($dst_img, $target); } else { imagejpeg($dst_img, $target); } imagedestroy($dst_img); imagedestroy($src_img); } |
||
![]() |
![]() |
Svara |
|
|