PHP image resize
Results 1 to 6 of 6

Thread: PHP image resize

  1. #1
    Member
    Join Date
    Oct 2002
    Posts
    52

    PHP image resize

    I'm having some issues putting 2 pieces of code together

    First part
    I have this string
    $string = "some text... <img src=myimage.jpg> some more text <img src=myimage.jpg>";

    What i want to do is add width and height to those img tags and automatically reduce the image size by like 50%. I have a function that returns the image width and height in an array. For example

    $resized_image = imgresize("path to image");
    I then can print $resized_image[height] to get the height, and $resized_image[width] for the width.

    Second part
    I have a reg exp that will search for the image tag and add width and height

    Code:
    $pattern = "/(<img\s+).*?src=((\".*?\")|(\'.*?\')|([^\s]*)).*?>/is";
    $replacement = "<img src=$2 width=$resized_image[width] height=$resized_image[height]>";
    
    echo preg_replace($pattern, $replacement, $string);
    How can i tie these 2 together? The only way i can think of is to call the array in the $replacement string, but dont know how to do so with an array

  2. #2
    Senior Member
    Join Date
    Oct 2003
    Location
    MA
    Posts
    1,053
    I would love to answer your question but it is so unclear what you are asking for..

  3. #3
    Member
    Join Date
    Oct 2002
    Posts
    52
    sorry...

    I have a database that has html content that i display on 2 different pages. In that database the content has several img tags. What i want to do is add width and height to the tag with a new size (i want to make it smaller). I have a function that will return the new size of the image in an array, and can add a fixed width and height to the img tag, but i cant get it to get the values from the function and put that in the width and height. So for example here is my code

    Code:
    $resized_image = imagereduction($2);
    $pattern = "/(<img\s+).*?src=((\".*?\")|(\'.*?\')|([^\s]*)).*?>/is";
    $replacement = "<img src=$2 width=$resized_image[width] height=$resized_image[height]>";
    
    echo preg_replace($pattern, $replacement, $string);
    I have also tried this

    Code:
    $pattern = "/(<img\s+).*?src=((\".*?\")|(\'.*?\')|([^\s]*)).*?>/is";
    $replacement = "<img src=$2 width=imagereduction_width($2) height=imagereduction_height($2)>";
    
    echo preg_replace($pattern, $replacement, $string);
    I'm not sure where the $2 comes from, i'm guessing it has something to do with preg_replace. I knew the first one wouldnt work, but the 2nd code should, at least i would think, but i'm no expert.
    Hope this is a bit clearer
    Last edited by Keith; October 13th, 2008 at 10:08 PM.

  4. #4
    Senior Member
    Join Date
    Oct 2003
    Location
    MA
    Posts
    1,053
    Well there are a few things...
    Assuming that imagereduction is a valid and working function already:

    the $2 variable is not declared any where?
    and $2 is not a valid variable in php - a variable must start with a letter..

  5. #5
    Member
    Join Date
    Oct 2002
    Posts
    52
    I'm not sure where $2 comes from but this works, i've tested it
    Code:
    $pattern = "/(<img\s+).*?src=((\".*?\")|(\'.*?\')|([^\s]*)).*?>/is";
    $replacement = "<img src=$2 width=50 height=50>";
    
    echo preg_replace($pattern, $replacement, $string);

  6. #6
    Senior Member
    Join Date
    Oct 2003
    Location
    MA
    Posts
    1,053
    I dont know then, Id have to see the full code..

Similar Threads

  1. Replies: 0
    Last Post: August 30th, 2007, 04:33 AM
  2. Apache, PHP, MySQL with basic security settings.
    By nightcat in forum The Security Tutorials Forum
    Replies: 9
    Last Post: May 28th, 2005, 02:47 AM
  3. Tutorial: Apache 2.0.49 and PHP 4.3.7!
    By Vorlin in forum Other Tutorials Forum
    Replies: 3
    Last Post: June 15th, 2004, 10:25 PM
  4. Installing Apache and PHP on Linux
    By HDD in forum Other Tutorials Forum
    Replies: 2
    Last Post: February 1st, 2004, 07:05 PM
  5. Protecting Yourself From Image Theft
    By GreekGoddess in forum The Security Tutorials Forum
    Replies: 19
    Last Post: August 28th, 2003, 06:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •