URL: https://www.overclockers.at/coding-stuff/php_help_gesucht_200031/page_2 - zur Vollversion wechseln!
Das heißt ich schreib eine eigene Seite, wo einfach 1x die Funktion drinnen ist. -> surfe ich die Seite an, macht der Server das ganze 1x.
ja?
hab jetzt das hier genommen:
Code: PHP<?PHP $image_name = "foo.jpg"; $style = "show"; // I've taken the foldernames. It's easier. For the //thumbnails replace "show" with "thumb". $image_name = "imagesdb/$style/$image_name"; if(!file_exists($image_name)) $image_name = "image.php?image_name=$image_name&style=$style"; // only if file doesn't exist call the on-the-fly creating file ?> Now the main script, stored in the file image.php: <?PHP $image_name = $_GET['image_name']; $style = $_GET['style']; // Now set the maximum sizes to the different styles. // You may set additional styles, but remember to // create the according subfolders. switch($style) { case "show": $max_size = 800; break; case "thumb": $max_size = 125; } $dest_file = "imagesdb/$style/$image_name"; // set output file $image_file = "imagesdb/full/$image_name"; // set source file $size = getimagesize($image_file); // get original size if($size[0] > $size[1]) { $divisor = $size[0] / $max_size; } else { $divisor = $size[1] / $max_size; } // to get allways pictures of the same size, which ist // mostly wanted in imageviewers, look what ist larger: // width or height $new_width = $size[0] / $divisor; $new_height = $size[1] / $divisor; // set new sizes settype($new_width, 'integer'); settype($new_height, 'integer'); // sizes should be integers $image_big = imagecreatefromjpeg($image_file); // load original image $image_small = imagecreatetruecolor($new_width, $new_height); // create new image imagecopyresampled($image_small, $image_big, 0,0, 0,0, $new_width,$new_height, $size[0],$size[1]); // imageresampled whill result in a much higher quality // than imageresized imagedestroy($image_big); // the original data are no longer used header("Content-type: image/jpeg"); if($style=="show" || $style=="thumb") { if(!file_exists($dest_file)) imagejpeg($image_small, $dest_file, 100); } // if you have set additional sizese put them in the // if-arguments, too. // if someone calls the image.php directly in the // browser with imagenames allready existing, they // won't be overwritten imagejpeg($image_small, '', 100); imagedestroy($image_small); // finally send image to browser and destroy no longer // needed data. ?>
Eine Frage bezüglich for-schleifen -> wie kann ich einen kompletten Ordner einlesen, sodass er mir JEDES Bild darin überprüft und gegebenenfalls verkleinert und nicht nur ein einziges?
Ich habe versucht ein Array zu machen, in das er mir die Filenames schreibt, die Stelle+Filename hätte ich zur Überprüfung genommen und genau danach durchlaufen lassen. Aber irgendwie speichert er mir kein Array über eine While/For-Schleife hinaus...
Anbei das Script:
Code: PHP<?PHP //function num_files($directory='.') { // return count(glob($directory."/*.*")); //} //this will find the number of files (only files) in "/horse/cat/" //$file_count = num_files("imagesdb/full/"); $string=""; $fileCount=0; $filePath=$PATH.'imagesdb/full/'; # Specify the path you want to look in. $dir = opendir($filePath); # Open the path while ($file = readdir($dir)) { if (eregi(".jpg",$file) && !eregi("._",$file) ) {# Look at only files with a .jpg extension $image_name = "$file"; } } $style = "thumb"; // I've taken the foldernames. It's easier. For the //thumbnails replace "show" with "thumb". $file_compare = "imagesdb/$style/$image_name"; if(!file_exists($file_compare)){ switch($style) { case "show": $max_size = 800; break; case "thumb": $max_size = 125; } $dest_file = "imagesdb/$style/$image_name"; // set output file $image_file = "imagesdb/full/$image_name"; // set source file $size = getimagesize($image_file); // get original size if($size[0] > $size[1]) { $divisor = $size[0] / $max_size; } else { $divisor = $size[1] / $max_size; } // to get allways pictures of the same size, which ist // mostly wanted in imageviewers, look what ist larger: // width or height $new_width = $size[0] / $divisor; $new_height = $size[1] / $divisor; // set new sizes settype($new_width, 'integer'); settype($new_height, 'integer'); // sizes should be integers $image_big = imagecreatefromjpeg($image_file); // load original image $image_small = imagecreatetruecolor($new_width, $new_height); // create new image imagecopyresampled($image_small, $image_big, 0,0, 0,0, $new_width,$new_height, $size[0],$size[1]); // imageresampled whill result in a much higher quality // than imageresized imagedestroy($image_big); // the original data are no longer used header("Content-type: image/jpeg"); if($style=="show" || $style=="thumb") { if(!file_exists($dest_file)) imagejpeg($image_small, $dest_file, 100); } // if you have set additional sizese put them in the // if-arguments, too. // if someone calls the image.php directly in the // browser with imagenames allready existing, they // won't be overwritten imagejpeg($image_small, '', 100); imagedestroy($image_small); // finally send image to browser and destroy no longer // needed data. $fileCount++; } else { echo "Nothing to convert!"; } ?>
wo verwendest du ein array?
Code: PHP$handle=opendir($dir); while (($file=readdir($handle))!==false) { if (!is_dir($file) && !file_exists("$dir/thumbs/sm_$file) { liest alle files in $dir, und wenn es kein thumb gibt bzw. das file kein ordner ist dann ... } }
Zitat von Smutwo verwendest du ein array?
Es darf keinerlei Output in dem Script das das Bild generieren soll geben, sonst wird entweder das Bild defekt sein oder eben der von dir beschriebene Fehler auftreten.Zitat von LTDDas Problem ist, er schmeisst mich beim Header raus. Sagt mir dass der Header nicht erneut erstellt werden konnte, erstellt ein einziges Bild und hört dann auf. Lösche ich dann den Part von Hader bis zum fileCount++ aus der Funktion, erstellt er mir keine Bilder mehr.
Zitat von LTD@smut Habe ich gekickt weil es ausserhalb der while-Schleife keine Werte mehr inne hatte.
@jives - ich geh davon aus, dass ich nur jpgs benutze =).
Wie funktioniert das mit dem Script? Ich habe mal versucht das jetzige Script aufzurufen, aber wenn ich dann die Seite mit dem Aufruf-Script lade, tut sich garnix... muss ihc da irgend was spezielles reinschreiben?
die !_GET hat in der Seite mit dem Verkleinerungsscript nix bekommen von der anderen (also zb. filename, path, usw.)
Kannst du deinen Code hier bitte nochmal posten?Zitat von LTDWie funktioniert das mit dem Script? Ich habe mal versucht das jetzige Script aufzurufen, aber wenn ich dann die Seite mit dem Aufruf-Script lade, tut sich garnix... muss ihc da irgend was spezielles reinschreiben?
die !_GET hat in der Seite mit dem Verkleinerungsscript nix bekommen von der anderen (also zb. filename, path, usw.)
Zitat von LTDCode: PHP<?PHP $image_name = "foo.jpg"; $style = "show"; // I've taken the foldernames. It's easier. For the //thumbnails replace "show" with "thumb". $image_name = "imagesdb/$style/$image_name"; if(!file_exists($image_name)) $image_name = "image.php?image_name=$image_name&style=$style"; // only if file doesn't exist call the on-the-fly creating file ?> Now the main script, stored in the file image.php: <?PHP $image_name = $_GET['image_name']; $style = $_GET['style']; // Now set the maximum sizes to the different styles. // You may set additional styles, but remember to // create the according subfolders. switch($style) { case "show": $max_size = 800; break; case "thumb": $max_size = 125; } $dest_file = "imagesdb/$style/$image_name"; // set output file $image_file = "imagesdb/full/$image_name"; // set source file $size = getimagesize($image_file); // get original size if($size[0] > $size[1]) { $divisor = $size[0] / $max_size; } else { $divisor = $size[1] / $max_size; } // to get allways pictures of the same size, which ist // mostly wanted in imageviewers, look what ist larger: // width or height $new_width = $size[0] / $divisor; $new_height = $size[1] / $divisor; // set new sizes settype($new_width, 'integer'); settype($new_height, 'integer'); // sizes should be integers $image_big = imagecreatefromjpeg($image_file); // load original image $image_small = imagecreatetruecolor($new_width, $new_height); // create new image imagecopyresampled($image_small, $image_big, 0,0, 0,0, $new_width,$new_height, $size[0],$size[1]); // imageresampled whill result in a much higher quality // than imageresized imagedestroy($image_big); // the original data are no longer used header("Content-type: image/jpeg"); if($style=="show" || $style=="thumb") { if(!file_exists($dest_file)) imagejpeg($image_small, $dest_file, 100); } // if you have set additional sizese put them in the // if-arguments, too. // if someone calls the image.php directly in the // browser with imagenames allready existing, they // won't be overwritten imagejpeg($image_small, '', 100); imagedestroy($image_small); // finally send image to browser and destroy no longer // needed data. ?>
ist klar, daß sich nix tut.
du überprüfst ob ein file existiert und falls ja, dann weißt du einer variablen einen wert zu. und das wars auch schon, dann ist das skript zu ende.
Dachte das wäre der Funktionsaufruf *g*
$image_name = "image.php?image_name=$image_name&style=$style";
Wie macht mans richtig?
Wenn ich das richtig verstehe willst du die Thumbs auch anzeigen, also müsse hier etwas in der Form
stehen.Code: PHPecho "<img src=\"image.php?image_name=$image_name&style=$style\" />";
Zitat von jivesWenn ich das richtig verstehe willst du die Thumbs auch anzeigen, also müsse hier etwas in der Formstehen.Code: PHPecho "<img src=\"image.php?image_name=$image_name&style=$style\" />";
overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2025