Support » Fixing WordPress » How to implement this handy PHP script

  • Resolved jamesb4551


    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?

    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.'”>’;

    return ‘<img src=”‘.$image.'” width=”‘.$width.'” height=”‘.$height.'”>’;

    Thanks a lot anyone who replies…

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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.

    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; ?>

    is this correct?

    Anyone? sorry this is kinda urgent….

    In your loop you’d have:

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

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

    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.

    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.

    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to implement this handy PHP script’ is closed to new replies.