WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to implement this handy PHP script (8 posts)

  1. jamesb4551
    Member
    Posted 5 years ago #

    Hey wordpress breadbins, I have a query. I have found what I hope to be a handy script to re-size all images proportionally. Until now I have used css to set a max width of 530px and height as auto, but the crazy cats over at moicrowswoft developed ie8 which seems to regognise the max width of 530px, but then if any image is put in at full size and has rather large dimensions, then it stretches the height all the way down to the pixel dimension stated in the post. Yes i could change this in the posts but that would mean going through 1000's of posts to do so. So my question is where would I need to put this script to implement it for the entire of my wordpress site?

    <?php
    function imageRestrict($image) {
    $maxwidth = 530;
    list($width,$height) = getimagesize($image);

    if ($width > $maxwidth) {
    $newheight = $maxwidth/$width * $height;
    return '<img src="'.$image.'" width="'.$maxwidth.'" height="'.$newheight.'">';
    }

    else{
    return '<img src="'.$image.'" width="'.$width.'" height="'.$height.'">';
    }
    }
    ?>

    Thanks a lot anyone who replies...

  2. Eric Mann
    Member
    Posted 5 years ago #

    Any php script you want to run on your blog needs to be placed in the functions.php file of your theme. Then you can call the function from anywhere else in the theme that you need.

  3. jamesb4551
    Member
    Posted 5 years ago #

    Thanks ericmann, so I put the above script anywhere in my functions.php then in my template in the page.php i would put

    ...
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div --->
    <?php if ( function_exists( 'imageRestrict' ) ) ImageRestrict( .$image ); ?>
    <?php endwhile; ?>
    </div>
    ...

    is this correct?

  4. jamesb4551
    Member
    Posted 5 years ago #

    Anyone? sorry this is kinda urgent....

  5. Eric Mann
    Member
    Posted 5 years ago #

    In your loop you'd have:

    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    <div>
    ...
    <?php if(function_exists('imageRestrict')) imageRestrict($image); ?>
    ...
    <?php endwhile; ?>
    ...
    </div>
    ...

    I don't know why you had a period before $image in your version above. But this *should* work just fine.

  6. jamesb4551
    Member
    Posted 5 years ago #

    Thanks Ericman, I did as you said but it seemed to generate an error and not load my blog, maybe I'm putting it in the wrong place. Would the

    <?php if(function_exists('imageRestrict')) imageRestrict($image); ?>

    need to go in page.php & index.php and that's it? Also would it matter if it was before any other functions or would it need to be after?
    sorry I'm new to PHP completely.

  7. Eric Mann
    Member
    Posted 5 years ago #

    That line is correct, and you would put it in several places:

    • To use it on your home page, place it in index.php
    • To use it on individual posts, place it in single.php
    • To use it on individual pages, place it in page.php

    One thing you need to make sure of first, though, you absolutely must define the $image variable before passing it to the function. If you don't it will cause an error (which is probably what is happening now). Without seeing your entire code, though, I can't determine what the actual problem is.

  8. jamesb4551
    Member
    Posted 4 years ago #

    In the end I found a 'see if GD library is working on your server' php script, and turns out even thought they said it was, it wasn't working for jpgs, which this script found out and so I told them to fix it and all is ok now. cheers for your help ericmann.

Topic Closed

This topic has been closed to new replies.

About this Topic