Need simple PHP Coding for Images on a Wordpress theme

Discussion in 'Programming' started by vinny, Jun 26, 2008.

  1. #1
    need php coding for auto image resizing.

    The max width i have is 500, sometimes i use images much wider... i need a code to auto adjust the images width to 500, and then auto adjust the height to the correct ratio. Thanks
     
    vinny, Jun 26, 2008 IP
  2. Jaybreeze

    Jaybreeze Peon

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    I can do this for you. how do you want this incorporated? Like in your posting function or gallery...... ect
     
    Jaybreeze, Jun 26, 2008 IP
  3. vinny

    vinny Peon

    Messages:
    268
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    this will be mainly for posting. Also does this distort the original image quality when the image is resized to fit?
     
    vinny, Jun 26, 2008 IP
  4. Gate

    Gate Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    Dont pay for this, it tells you how to do it on the PHP site!

    And theres and example script!

    Search this forum, someone posted links to it
     
    Gate, Jun 27, 2008 IP
  5. vinny

    vinny Peon

    Messages:
    268
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    which PHP site ? are you referring to a php sub forum?
     
    vinny, Jun 27, 2008 IP
  6. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #6
    I'll give you an HTML / CSS fix :) .

    For each image that you are using please use this:

    <img src="image link" class="imgM" />
    HTML:
    Then add this into your CSS file:

    .imgM {
      max-width: 500px;
      max-height: 500px;
    }
    HTML:
    That should help you out.
     
    Riverofrhyme, Jun 27, 2008 IP
  7. Ohad

    Ohad Guest

    Messages:
    622
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #7
    As far as I know IE 6 doesn't support this.
    I can do this script, but I am not sure how to make it work with wordpress..
    MSN:
     
    Ohad, Jun 27, 2008 IP
  8. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #8
    Ok then, this one works for new browsers and IE 5.0 + (apparantly)
    For each image that you are using please use this:

    <img src="image link" class="imgM" />
    HTML:
    Then add this into your CSS file:

    .imgM {
      max-width: 500px;
      max-height: 500px;
      width:expression( 
        document.body.clientWidth > (500/12) * 
        parseInt(document.body.currentStyle.fontSize)?
            "30em":
            "auto" );
    }
    HTML:
    http://www.svendtofte.com/code/max_width_in_ie/
     
    Riverofrhyme, Jun 27, 2008 IP
  9. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #9
    Do you want your images physically resized or just visually resized?

    With the former bandwidth will be reduced and page load times will decrease but the resized image will be stored on your server. With the latter, however, bandwidth use and load times will remain the same but there is no overhead of creating a copy of the image locally.

    The latter is the quickest and easier method to implement but both are possible. I could do either for you if you wish. You would not need to assign classes or do anything to images either, it would be done automatically.
     
    clarky_y2k3, Jun 27, 2008 IP
  10. vinny

    vinny Peon

    Messages:
    268
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #10
    Thank you sir ill try it

     
    vinny, Jun 27, 2008 IP
  11. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #11
    The extra ie5.0 + will only work for width, if you want to make it work for height as well, try this:

    .imgM {
      max-width: 500px;
      max-height: 500px;
      width:expression( 
        document.body.clientWidth > (500/12) * 
        parseInt(document.body.currentStyle.fontSize)?
            "30em":
            "auto" );
      height:expression( 
        document.body.clientHeight > (500/12) * 
        parseInt(document.body.currentStyle.fontSize)?
            "30em":
            "auto" );
    }
    HTML:
    That's not tested though.

    This way no PHP is needed, also, you don't have to assign the class either, just use the CSS img {} and that would work.
     
    Riverofrhyme, Jun 27, 2008 IP
  12. vinny

    vinny Peon

    Messages:
    268
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #12
    I only need it for the width. Im a little confused. is this something that i just add once and then the resizing becomes automated? or do i have to do this for each single image i upload?

    thanks again, youve been very helpful

     
    vinny, Jun 27, 2008 IP
  13. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #13
    Ok, just for width, you can try this. I have also changed it so you don't need to add any classes to your images, so it will work straight away.

    img {
      max-width: 500px;
      width:expression( 
        document.body.clientWidth > (500/12) * 
        parseInt(document.body.currentStyle.fontSize)?
            "30em":
            "auto" );
    }
    HTML:
     
    Riverofrhyme, Jun 27, 2008 IP
  14. Calibula

    Calibula Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #14
    But that will add the CSS to EVERY image in the site. If he have a banner, it will resize it.
    He should probably just need to use class..
     
    Calibula, Jun 27, 2008 IP
  15. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #15
    If he doesn't want it to apply to his banner, then he will need to use my first option.
     
    Riverofrhyme, Jun 27, 2008 IP
  16. rochow

    rochow Notable Member

    Messages:
    3,991
    Likes Received:
    245
    Best Answers:
    0
    Trophy Points:
    240
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #16
    Resizing images using height+width reduces the quality, especially for the fact the width might change, but the height won't (so not only will it be distorted due to sizing, but squashed horizontally)

    If it was PHP resized it would come out better (as in it actually resized - it takes the old image, generates a new one - so if the width is 75% then the height is 75% also, and then that overwrites the old image)
     
    rochow, Jun 27, 2008 IP
  17. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #17
    max-width and max-height resizes smartly. It keeps the images in the same ratio of width:height.
     
    Riverofrhyme, Jun 27, 2008 IP
  18. rochow

    rochow Notable Member

    Messages:
    3,991
    Likes Received:
    245
    Best Answers:
    0
    Trophy Points:
    240
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #18
    Here's an image resized using your method. Original on the left, resized on the right.

    [​IMG]
    http://img57.imageshack.us/img57/6850/resizegv6.jpg

    Thats using a 100% quality JPG image (both the Ninja, and the image I have above) That's a small image too - if it was a big image then you would be able to see the distortion even more.

    Like I said, the quality isn't good. Unless you want to argue that it is of course...

    EDIT: It does seem to resize both ways, last time I used HTML or CSS to resize images was before CSS2 was even supported by the browser on my powerhouse Windows 95 machine. The quality still looks as rubbish as it did back then though (some things never seem to advance quickly), so its not as good a solution as PHP.
     
    rochow, Jun 27, 2008 IP