• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Bildgrösse mit PHP ermitteln

  • Ersteller Ersteller matibaski
  • Erstellt am Erstellt am
Status
Für weitere Antworten geschlossen.
M

matibaski

Guest
Moin!
Ich möchte für mein GB einen Image TAG verwenden.
Doch wie ermittle ich die Grösse?

Also bis jetzt sieht der Code so aus:
PHP:
$inhalt = preg_replace("%\[img\](.*)\[\/img]%isU", "<img src=\"$1\" alt=\"\"  style=\"width:??px; height:??px; border:0px;\" />",$inhalt);
Wie kann ich noch ausgeben, ob die Breite/Höhe zu gross ist?
Das Bild darf eben nicht breiter als 300px sein.

Bin für jede Hilfe dankbar.



MfG, matibaski
 
Di Größe kriegts du mit getimagesize.
Wenn du also
PHP:
$bild = getimagesize('pfad/bild.jpg');
schreibtst, dann bekommst du
Code:
$bild[0] die Breite in Pixel (z.B. 300)
$bild[1] die Höhe  in Pixel (z.B. 200)
$bild[2] den Datei-Typ (z.B. JPG)
$bild[3] den Tag-Teil: [I]height=200 width=300[/I]
Ich hoffe das ist das, was du wissen wolltest, mehr weiß ich nicht.
 
Danke.
Also jetzt für die, die das evtl. mal brauchen werden, und fals ich es falsch hätte:
PHP:
$image = getimagesize(('bild.ext');

if($image[0] > 300) // Wenn das Bild breiter als 300px ist.
{
 die("Das Bild ist zu breit"); // Error
}
elseif($image[1] > 300) // Wenn das Bild höher als 300px ist
{
 die("Bild ist zu hoch"); // Error
}
So weit, so gut.
Doch wie kann ich das nun mit meinem preag_replace verbinden?
Hat jemand ne Idee?


MfG, matibaski

PS: Hab ich oben was falsch gemacht? Danke für Info, wenn was falsch ist.
 
Hallo,

mit dem e Modifier kannste eine Funktion im zu ersetzenden Teil aufrufen. In der Funktion führst du deinen Test durch, ist das Bild zu groß fügste ein anderes ein (über den Rückgabewert der Funktion).

Code:
/regex/e
Siehe auch: Example 1699. Using the 'e' modifier

N43
 
hm oder wenns dir um das ersetzen ging
PHP:
list($width,$height,$type,$tag) = getimagesize('bild.ext');
if(max($width,$height)>300)
  die("Das Bild ist zu groß! Maximale Größe: 300x300px")
$pattern = "%\[img\](.*)\[\/img]%isU";
$replace = "<img src=\"$1\" alt=\"\" ".$tag." border=\"0px\" />";
$inhalt = preg_replace($pattern, $replace,$inhalt);
 
Danke, ja genau sowas.
Lässt sich das nicht iwie in CSS haun?
Also style="".


MfG, matibaski
 
doch klar ^^
PHP:
$replace = "<img src=\"$1\" alt=\"\" style=\"width: ".$width.";height:".$height.";border:0;\" />";
 
LoL.
Hab n vollen Kopf mit Codes usw...
Hätte ich selber rausfinden sollen.
Danke.

Nun meine Frage. Mit der Funktion getimagesize() wird das Bild gecheckt.
Doch wie bringe ich alle Bilder dort rein, also die URL's, die eingegen wurden?



MfG, matibaski
 
tja dann musst du doch auf die variante mit dem modifier e zurückgreifen ;)
PHP:
function bild($url = false)
{
  if(!$url)
    return false;
  list($width,$height,$type) = getimagesize($url);
  if(max($width,$height) > 300)
    return('Ihr Bild "'.$url.'" ist zu groß! Maximale Größe: 300x300px');
  return('<img src="'.$url.'" alt="" style="width: '.$width.';height:'.$height.';border:0;" />'); 
}
$patter = '%\[img\](.*)\[\/img]%isUe';
$inhalt = preg_replace($pattern,"bild('$1')",$inhalt);
 
Danke @ Frank, doch gibt n Error aus.
Also so siehts aus:
PHP:
       function bild($url = false)
       {
        if(!$url)
        {
         return false;
        }
        list($width,$height,$type) = getimagesize($url);
        if(max($width,$height) > 300)
        {
         return('Ihr Bild "'.$url.'" ist zu groß! Maximale Größe: 300x300px');
        }
        return('<img src="'.$url.'" alt="" style="width: '.$width.';height:'.$height.';border:0;" />');
       }
       $smiley = preg_replace("%\[img\](.*)\[\/img]%isUe","bild('$1')",$smiley);
Es kommt folgender Error:
Fatal error: Cannot redeclare bild() (previously declared in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/g-book.php:64) in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/g-book.php on line 64

Also Line 64 ist function bild($url = false).

Es steht, dass es schon auf Zeile 64 deklariert worden ist, was aber auf derselben Zeile ist. Ich kapier den Error nich.
Wer kapiert den?


MfG, matibaski
 
Ne hab ich nicht.
Zur Überzeugung, der ganze Quelltext des Gästebuches:
PHP:
<?php
/* Connecting */
#####################
// Daten
######################

@mysql_connect($host, $user, $pass) OR die(mysql_error());
@mysql_select_db($db) OR die("ERROR\n".mysql_error());
/* ENDE Connecting */

echo '<h1>Gästebuch</h1>
      <span style="float:right; margin-bottom:5px;">
       <a style="margin-bottom:5px;" href="index.php?site=gb_add">Eintrag erstellen</a>
      </span>';
    
// Auslesen der Einträge
$sql = "SELECT
            Name,
            Datum,
            Email,
            Homepage,
            Inhalt,
            Kommentar,
            ID
        FROM
            gaestebuch
        ORDER BY
            Datum DESC";

$result = mysql_query($sql) OR die(mysql_error());

// Ausgeben der Anzahl von Einträgen
echo'Einträge im Gästebuch: '.mysql_num_rows($result)."\n".
     '<div style="border:0px; border-bottom:1px solid black; width:100%; height:1%; margin-top:5px;"></div>'."\n";

while($row = mysql_fetch_assoc($result))
{
 echo '<div>'."\n".
      '<div style="font-weight:bold; vertical-align:middle; margin-top:5px;">'."\n".
      '<img src="http://www.html.de/images/icons/post.gif" style="vertical-align:middle; width:14px; height:13px; border:0px;" alt="Post von '.$row['Name'].'" /> '.$row['Name'].' schrieb um '.$row['Datum']."\n".
      '</div>'."\n".
      '<div style="clear:both; display:block;">'."\n";
       // Smiley umwandlung 
       $smiley = nl2br($row['Inhalt']);
       $smiley = str_replace(":)", "<img src=\"http://www.html.de/images/gbook/smile.gif\">", $smiley);
       $smiley = str_replace(";)", "<img src=\"http://www.html.de/images/gbook/wink.gif\">", $smiley);
       $smiley = str_replace(":D", "<img src=\"http://www.html.de/images/gbook/laughing.gif\">", $smiley);
       $smiley = str_replace(":P", "<img src=\"http://www.html.de/images/gbook/tongue.gif\">", $smiley);
       $smiley = str_replace(":O", "<img src=\"http://www.html.de/images/gbook/wassat.gif\">", $smiley);
       $smiley = str_replace(":S", "<img src=\"http://www.html.de/images/gbook/sad.gif\">", $smiley);
       $smiley = str_replace(":(", "<img src=\"http://www.html.de/images/gbook/angry.gif\">", $smiley);
       $smiley = str_replace(":Q", "<img src=\"http://www.html.de/images/gbook/crying.gif\">", $smiley);
       $smiley = str_replace(":smile:", "<img src=\"http://www.html.de/images/gbook/smile.gif\">", $smiley);
       $smiley = str_replace(":wink:", "<img src=\"http://www.html.de/images/gbook/wink.gif\">", $smiley);
       $smiley = str_replace(":laughing:", "<img src=\"http://www.html.de/images/gbook/laughing.gif\">", $smiley);
       $smiley = str_replace(":tongue:", "<img src=\"http://www.html.de/images/gbook/tongue.gif\">", $smiley);
       $smiley = str_replace(":wassat:", "<img src=\"http://www.html.de/images/gbook/wassat.gif\">", $smiley);
       $smiley = str_replace(":sad:", "<img src=\"http://www.html.de/images/gbook/sad.gif\">", $smiley);
       $smiley = str_replace(":angry:", "<img src=\"http://www.html.de/images/gbook/angry.gif\">", $smiley);
       $smiley = str_replace(":crying:", "<img src=\"http://www.html.de/images/gbook/crying.gif\">", $smiley);
       function bild($url = false)
       {
        if(!$url)
        {
         return false;
        }
        list($width,$height,$type) = getimagesize($url);
        if(max($width,$height) > 300)
        {
         return('Ihr Bild "'.$url.'" ist zu groß! Maximale Größe: 300x300px');
        }
        return('<img src="'.$url.'" alt="" style="width: '.$width.';height:'.$height.';border:0;" />');
       }
       $smiley = preg_replace("%\[img\](.*)\[\/img]%isUe","bild('$1')",$smiley);
       $smiley = preg_replace("%\[url\](.*)\[\/url]%isU", "<a href=\"http://$1\">$1</a>",$smiley);
 echo '<br />'.$smiley."\n";
       // Kommentar vom Admin checken
       if(trim($row['Kommentar'] != ""))
       {
        echo '  <div style="font-size:9px; margin-top:15px; margin-bottom:15px;"><i><b>Admin Kommentar:</b></i><br />'.nl2br($row['Kommentar']).'</div>';
       }
       // Email abchecken
       if(trim($row['Email']) == '')
       {
        echo '';
       }
       else
       {
        echo '<a href="mailto:'.$row['Email'].'" alt="Email von '.$row['Name'].'"><img src="http://www.html.de/images/icons/kontakt.gif" style="width:16px; height:16px; border:0px;" alt="Homepage von '.$row['Name'].'" /></a>'."\n";
       }
       // Homepage abchecken
       if(trim($row['Homepage']) != '')
       {
        if(strtolower(substr($row['Homepage'], 0, 7)) == 'http://')
        {
         echo ' <a href="'.$row['Homepage'].'"><img src="http://www.html.de/images/icons/home.gif" style="width:16px; height:16px; border:0px;" alt="Homepage von '.$row['Name'].'" /></a> ';
        }
        else
        {
         echo ' <a href="http://'.$row['Homepage'].'"><img src="http://www.html.de/images/icons/home.gif" style="width:16px; height:16px; border:0px;" alt="Homepage von '.$row['Name'].'" /></a> ';
        }
       }
 echo'<div style="float:right; margin-bottom:5px;"><a style="text-decoration:none;" href="index.php?site=#top"><img src="http://www.html.de/images/icons/top.gif" style="width:7px; height:8px; border:0px;" alt="" />&nbsp;Nach oben</a></div>';
 echo'<div style="width:100%; height:1px; border:0px; border-bottom:1px solid #000000; margin-bottom:7px; margin-top:20px;"></div>'."\n".
     '</div>'."\n".
    '</div>';
}
?>
Sieht doch normal aus, oder?
Oder mache ich was falsch?


MfG, matibaski
 
du darfst die funktion nicht in der schleife deklarieren, sonst wird sie ja bei jedem aufruf neu deklariert, und das erzeugt den fehler.
PHP:
   <?php
function bild($url = false)
       {
        if(!$url)
        {
         return false;
        }
        list($width,$height,$type) = getimagesize($url);
        if(max($width,$height) > 300)
        {
         return('Ihr Bild "'.$url.'" ist zu groß! Maximale Größe: 300x300px');
        }
        return('<img src="'.$url.'" alt="" style="width: '.$width.';height:'.$height.';border:0;" />');
       }

/* Connecting */
#####################
// Daten
######################

@mysql_connect($host, $user, $pass) OR die(mysql_error());
@mysql_select_db($db) OR die("ERROR\n".mysql_error());
/* ENDE Connecting */

echo '<h1>Gästebuch</h1>
      <span style="float:right; margin-bottom:5px;">
       <a style="margin-bottom:5px;" href="index.php?site=gb_add">Eintrag erstellen</a>
      </span>';
    
// Auslesen der Einträge
$sql = "SELECT
            Name,
            Datum,
            Email,
            Homepage,
            Inhalt,
            Kommentar,
            ID
        FROM
            gaestebuch
        ORDER BY
            Datum DESC";

$result = mysql_query($sql) OR die(mysql_error());

// Ausgeben der Anzahl von Einträgen
echo'Einträge im Gästebuch: '.mysql_num_rows($result)."\n".
     '<div style="border:0px; border-bottom:1px solid black; width:100%; height:1%; margin-top:5px;"></div>'."\n";

while($row = mysql_fetch_assoc($result))
{
echo '<div>'."\n".
      '<div style="font-weight:bold; vertical-align:middle; margin-top:5px;">'."\n".
      '<img src="http://www.html.de/images/icons/post.gif" style="vertical-align:middle; width:14px; height:13px; border:0px;" alt="Post von '.$row['Name'].'" /> '.$row['Name'].' schrieb um '.$row['Datum']."\n".
      '</div>'."\n".
      '<div style="clear:both; display:block;">'."\n";
       // Smiley umwandlung
       $smiley = nl2br($row['Inhalt']);
       $smiley = str_replace(":)", "<img src=\"http://www.html.de/images/gbook/smile.gif\">", $smiley);
       $smiley = str_replace(";)", "<img src=\"http://www.html.de/images/gbook/wink.gif\">", $smiley);
       $smiley = str_replace(":D", "<img src=\"http://www.html.de/images/gbook/laughing.gif\">", $smiley);
       $smiley = str_replace(":P", "<img src=\"http://www.html.de/images/gbook/tongue.gif\">", $smiley);
       $smiley = str_replace(":O", "<img src=\"http://www.html.de/images/gbook/wassat.gif\">", $smiley);
       $smiley = str_replace(":S", "<img src=\"http://www.html.de/images/gbook/sad.gif\">", $smiley);
       $smiley = str_replace(":(", "<img src=\"http://www.html.de/images/gbook/angry.gif\">", $smiley);
       $smiley = str_replace(":Q", "<img src=\"http://www.html.de/images/gbook/crying.gif\">", $smiley);
       $smiley = str_replace(":smile:", "<img src=\"http://www.html.de/images/gbook/smile.gif\">", $smiley);
       $smiley = str_replace(":wink:", "<img src=\"http://www.html.de/images/gbook/wink.gif\">", $smiley);
       $smiley = str_replace(":laughing:", "<img src=\"http://www.html.de/images/gbook/laughing.gif\">", $smiley);
       $smiley = str_replace(":tongue:", "<img src=\"http://www.html.de/images/gbook/tongue.gif\">", $smiley);
       $smiley = str_replace(":wassat:", "<img src=\"http://www.html.de/images/gbook/wassat.gif\">", $smiley);
       $smiley = str_replace(":sad:", "<img src=\"http://www.html.de/images/gbook/sad.gif\">", $smiley);
       $smiley = str_replace(":angry:", "<img src=\"http://www.html.de/images/gbook/angry.gif\">", $smiley);
       $smiley = str_replace(":crying:", "<img src=\"http://www.html.de/images/gbook/crying.gif\">", $smiley);
       $smiley = preg_replace("%\[img\](.*)\[\/img]%isUe","bild('$1')",$smiley);
       $smiley = preg_replace("%\[url\](.*)\[\/url]%isU", "<a href=\"http://$1\">$1</a>",$smiley);
echo '<br />'.$smiley."\n";
       // Kommentar vom Admin checken
       if(trim($row['Kommentar'] != ""))
       {
        echo '  <div style="font-size:9px; margin-top:15px; margin-bottom:15px;"><i><b>Admin Kommentar:</b></i><br />'.nl2br($row['Kommentar']).'</div>';
       }
       // Email abchecken
       if(trim($row['Email']) == '')
       {
        echo '';
       }
       else
       {
        echo '<a href="mailto:'.$row['Email'].'" alt="Email von '.$row['Name'].'"><img src="http://www.html.de/images/icons/kontakt.gif" style="width:16px; height:16px; border:0px;" alt="Homepage von '.$row['Name'].'" /></a>'."\n";
       }
       // Homepage abchecken
       if(trim($row['Homepage']) != '')
       {
        if(strtolower(substr($row['Homepage'], 0, 7)) == 'http://')
        {
         echo ' <a href="'.$row['Homepage'].'"><img src="http://www.html.de/images/icons/home.gif" style="width:16px; height:16px; border:0px;" alt="Homepage von '.$row['Name'].'" /></a> ';
        }
        else
        {
         echo ' <a href="http://'.$row['Homepage'].'"><img src="http://www.html.de/images/icons/home.gif" style="width:16px; height:16px; border:0px;" alt="Homepage von '.$row['Name'].'" /></a> ';
        }
       }
echo'<div style="float:right; margin-bottom:5px;"><a style="text-decoration:none;" href="index.php?site=#top"><img src="http://www.html.de/images/icons/top.gif" style="width:7px; height:8px; border:0px;" alt="" />&nbsp;Nach oben</a></div>';
echo'<div style="width:100%; height:1px; border:0px; border-bottom:1px solid #000000; margin-bottom:7px; margin-top:20px;"></div>'."\n".
     '</div>'."\n".
    '</div>';
}
?>
 
LoooL.
Mann wieso fallen mir nie solche Überlegungsfehler ein?
Ich suche da die kompliziersten Lösungen, wobei man nur nachdenken muss :D.

Es funzt teilweise.
Wie soll ich sagen, schauts euch an:
MyB - G-book

Es stieht, dass es iwie nicht überprüfen kann, oder verstehe ich was falsch?



MfG, matibaski
 
ja da müsste das eigtl einzustellen sein.
Und falls du da nicht rankommst, habe ich eine Variante geproggt, die dein Problem umgeht.

PHP:
error_reporting(E_ALL);
function bild($url)
{
  /** Config **/
  $allowed_extensions = "(jpg|jpeg|gif|png|bmp)";
  $temporary_dir = "tmp/";
  /** /Config **/

  $pattern = "%^(.+)\.".$allowed_extensions."$%i";
  $replace = ".\\2";
  if(!preg_match($pattern,$url))
  {
    return "Invalid Image Type (preg_match)";
  }
  $extension = preg_replace($pattern,$replace,$url);
  $basename = basename($url,$extension);
  $basename = md5($basename);
  $basename = substr($basename,0,6);
  $imagecontent = file($url);
  $new_filename = $temporary_dir.$basename.$extension;
  $new_image = fopen($new_filename,"w");
  foreach($imagecontent as $new_line)
    fwrite($new_image,$new_line);
  fclose($new_image);
  list($width,$height) = getimagesize($new_filename);
  unlink($new_filename);
  if(!is_int($width)||!is_int($height))
    return "Invalid Image Type (getimagesize)";
  if(max($width,$height) > 300)
    return "Ihr Bild ist zu groß! Maximale Größe: 300*300px";
  return "<img src=\"".$url."\" alt=\"\" style=\"width: ".$width.";height:".$height.";border:0;\" />";
}
 
1. Danke für den ultrakomplizierten Code. Habe mir den ein paar mal den Quelltext durchgesehen und nicht viel kapiert.

2. Habs trotzdem ausprobiert, doch ergibt einen haufen Errors:
Warning: file() [webhosting24 - Login]: URL file-access is disabled in the server configuration in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/gb_insert.php on line 57

Warning: file(http://matibaski.paradoxe.de/MyB/images/logo.gif) [webhosting24 - Login]: failed to open stream: no suitable wrapper could be found in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/gb_insert.php on line 57

Warning: fopen(tmp/96d6f2.gif) [webhosting24 - Login]: failed to open stream: No such file or directory in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/gb_insert.php on line 59

Warning: Invalid argument supplied for foreach() in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/gb_insert.php on line 60

Warning: fclose(): supplied argument is not a valid stream resource in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/gb_insert.php on line 62

Warning: getimagesize() [webhosting24 - Login]: Unable to access tmp/96d6f2.gif in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/gb_insert.php on line 63

Warning: getimagesize(tmp/96d6f2.gif) [webhosting24 - Login]: failed to open stream: No such file or directory in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/gb_insert.php on line 63

Warning: unlink(tmp/96d6f2.gif) [webhosting24 - Login]: No such file or directory in /home/httpd/vhosts/paradoxe.de/subdomains/matibaski/httpdocs/MyB/_private/gb_insert.php on line 64

Wie's aussieht muss ich ich in der php.ini was ändern.
Wenn ihr mir oder jemand sagen kann, wo und was, dann werd ich das meinem Kollegen, der Hoster, sagen, und der würde es vielleicht ändern.


MfG, matibaski
 
naja ich hatte gehofft das file() bei dir anderes konfiguriert ist als getimagesize() und somit die Erlaubnis besitzt, auf externe Dateien zuzugreifen. Der Code macht dann eigtl. nichts weiter als die Datei auf den eigenen Server zu kopieren, dann mit getimagesize() die Angaben abzufragen und die kopie wieder zu löschen. ;)

Da aber file() auch nicht auf externe Dateien zugreifen darf musst du in die php.ini. Aber frag mich nicht wohin :oops:
 
Status
Für weitere Antworten geschlossen.
Zurück
Oben