Link to home
Start Free TrialLog in
Avatar of astubblefield
astubblefield

asked on

Modifying Image Name in PHP

I have the DEVELOPER TOOL BOX for DREAMWEAVER CS3 in PHP.
Using the Create Thumbnail it creates a thumbnail with the name of FILENAME_wxh.jpg, or myimage_80x80.jpg, the name of the image is stored in a database as myimage.jpg.
I am creatiing a XML file and i need to be able to take the database image name of myimage.jpg and modify the string to myimage_80x80.jpg.
So what i need is to find out how in PHP that i can modify the name myimage.jpg to  myimage_80x80.jpg, by some how inserting the _80x80 between the name and the extension.
I was told that it would be something like, <img src="(<?php echo $row_Recordset1['Image']; ?>)_80x80, true)" />
Avatar of administradores
administradores

echo '<img src="' . substr($row['Image'], 0, (strlen($row_Recordset1['img_name']) - 4)) . '_80x80' . strrchr($row_Recordset1['Image'], '.') . '" />';

an example:

<?php

$link = mysql_connect('localhost', 'username', 'userpass');

if ($link)
{
      if (@mysql_select_db('dbname', $link))
      {
            $sql = 'SELECT * FROM images_table';
            
            $result = mysql_query($sql);
            
            while ($row = mysql_fetch_assoc($result))
            {
                  echo '<img src="' . substr($row['Image'], 0, (strlen($row_Recordset1['img_name']) - 4)) . '_80x80' . strrchr($row_Recordset1['Image'], '.') . '" />';
            }
      }
      else
      {
            echo 'no database found';
      }
}
else
{
      echo 'not connected';
}


?>
I couldn't test the above code, so there are 2 mistakes i see, replace all $row vars to $row_Recordset1.

and also you can change the number 4 for this strlen(strrchr($row_Recordset1['Image'], '.'))

hope it helps
Avatar of astubblefield

ASKER

I'm not quite sure, but here is what I came up with form you post, when I view it at http://realestate.stunis.com/test.php and right click the properties I get, " http://realestate.stunis.com/uploads/thumbnails/_80x80

Here is the code of the page:

<?php require_once('Connections/dbRealestate.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_dbRealestate, $dbRealestate);
$query_Recordset1 = "SELECT id_propGen, (Image) AS PHOTO FROM propGeneral WHERE id_propGen = 16388";
$Recordset1 = mysql_query($query_Recordset1, $dbRealestate) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p><?php echo '<img src="uploads/thumbnails/' . substr($row['Image'], 0, (strlen($row_Recordset1['PHOTO']) - 4)) . '_80x80' . strrchr($row_Recordset1['Image'], '.') . '" />'; ?> (Thumbnail)</p>

<p><?php echo $row_Recordset1['PHOTO']; ?> (File Name)</p>
<p>
 
  <img src="uploads/<?php echo $row_Recordset1['PHOTO']; ?>" /> (Full Size Image)</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Thanks
ASKER CERTIFIED SOLUTION
Avatar of MasonWolf
MasonWolf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That above example should work and is shorter.
I tried these 4 with out luck,

<?php echo str_replace(".jpg","_80x80.jpg",$row['PHOTO']); ?>

<?php echo str_replace(".jpg","_80x80.jpg","$row['PHOTO']"); ?>

<?php echo str_replace(".jpg","_80x80.jpg",$row_Recordset1['PHOTO']); ?>

<?php echo str_replace(".jpg","_80x80.jpg","$row_Recordset1['PHOTO']"); ?>

I looked up the str_replace and it looks to me as if one of those should work, but something is just off a little and I can't find it.

Thanks,
If  I try <?php echo str_replace(".jpg","_80x80.jpg","testimage.jpg"); ?> , I get testimage_80x80.jpg, it is when I add the $row_Recordset1['PHOTO'], <?php echo str_replace(".jpg","_80x80.jpg","$row_Recordset1['PHOTO']"); ?> that it breaks.

What is an example of the value in $row['PHOTO']?

Try:
echo $row['PHOTO'];

See what you get. Does it end in ".jpg"? There might be a problem there.

By the way, inside a quoted string, you would remove the single-quote from around the array key.

echo "$row_Recordset1[PHOTO]"; //is correct
echo "$row_Recordset1['PHOTO']"; //is incorrect
echo $row_Recordset1['PHOTO']; //is correct
echo $row_Recordset1[PHOTO]; //is possibly valid, but is insecure and should not be used
JI don't know why but it started working with <?php echo str_replace(".jpg","_80x80.jpg",$row_Recordset1['PHOTO']); ?>