Jump to content

Dividing $obj->width() output by 2?


Chrisnaj

Recommended Posts

Hello.

I'm not a PHP coder (more frontend dev) but I've been tasked with tweaking some PHP.

Specifically, the client wants to upload double-sized images but still get the image to display at the original size.

At the moment, when an image is uploaded (via a custom image manager) the width of the image is noted and an inline style is generated within a file called ajax_actions.php:

$obj = new Image($id);

<img src="'.$obj->src().'" title="'.$obj->artist().'" style="max-width:'.$obj->width().'px;"/>

The code I'm interested in is this:

style="max-width:'.$obj->width().'px;"

Is there any way of introducing a calculation to halve the size of the uploaded image into the empty brackets following width, something like: 

$obj->width(/2)

?

I can't, at the moment, do any tests on the file myself, so I'm looking for information as to how it should be done.

If I haven't supplied enough info please let me know.

Any help would be greatly appreciated!

 

 

Link to comment
Share on other sites

Images tend to be uploaded once but download for display many times. Thus the most efficient method is to create a display-sized image when the file is uploaded and store both. When required for output, get the display-sized image instead of the original. This results in smaller files hence faster loads.

Edited by Barand
Link to comment
Share on other sites

Here it is (thanks!):

<?php
if(isset($_REQUEST['action'])){ $action = $_REQUEST['action']; } else { echo 'ERROR'; exit; }

if($action == 'MORE_IMAGES'){
    if(isset($_REQUEST['ids'])){ $ids = explode(',', $_REQUEST['ids']); } else { echo 'ERROR'; exit; }
    if(isset($_REQUEST['category'])){ $category = $_REQUEST['category']; } else { echo 'ERROR'; exit; }
    if(isset($_REQUEST['category_id'])){ $category_id = $_REQUEST['category_id']; } else { echo 'ERROR'; exit; }
    if(isset($_REQUEST['from'])){ $from = $_REQUEST['from']; } else { echo 'ERROR'; exit; }

    $list = new Images($category);
    $list->query_list($ids);

    $list->reset(); 
    $i = $from; 
    while(list($id, $obj) = $list->each()){     
        echo '<img id="thumbnail-'.$i++.'" class="thumbnail" src="'.$obj->tsrc().'" title="'.$obj->artist().'" data-id="'.$obj->id().'"/>';
    }
} else if($action == 'IMAGE_DATA'){
    if(isset($_REQUEST['id'])){ $id = $_REQUEST['id']; } else { echo 'ERROR'; exit; }
    
    $sql = '
        SELECT nb_artist.nb_artist_name as name, '.sql_clean_href('nb_artist.nb_artist_name').' as href 
        FROM nb_artist LEFT JOIN nb_image_artist USING (nb_artist_id) WHERE nb_image_id = "'.$id.'"
    ';
    $rows = $GLOBALS['IDB']->get_results($sql, ARRAY_A);
    $artist = '<a href="/'.$rows[0]['href'].'">'.$rows[0]['name'].'</a>';
    
    $data = '';
    $styles = new Styles();
    $sql = '
        SELECT nb_style.nb_style_name as name, '.sql_clean_href('nb_style.nb_style_name').' as href
        FROM nb_style LEFT JOIN nb_image_style USING (nb_style_id) WHERE nb_image_id = "'.$id.'"
    ';
    $rows = $GLOBALS['IDB']->get_results($sql, ARRAY_A);
    foreach($rows as $row){
        if($styles->exists_by_href($row['href'])){
            $data .= '<li><a href="/'.$row['href'].'">'.$row['name'].'</a></li>';
        }
    }

    $subjects = new Subjects();
    $sql = '
        SELECT nb_subject.nb_subject_name as name, '.sql_clean_href('nb_subject.nb_subject_name').' as href
        FROM nb_subject LEFT JOIN nb_image_subject USING (nb_subject_id) WHERE nb_image_id = "'.$id.'"
    ';
    $rows = $GLOBALS['IDB']->get_results($sql, ARRAY_A);
    foreach($rows as $row){
        if($subjects->exists_by_href($row['href'])){
            $data .= '<li><a href="/'.$row['href'].'">'.$row['name'].'</a></li>';
        }
    }
    $obj = new Image($id);
    echo '
    <img id="prev-image" src="/wp-content/themes/nb/css/bg/arrow-previous.png">
    <img id="next-image" src="/wp-content/themes/nb/css/bg/arrow-next.png">
    <figure>
        <img src="'.$obj->src().'" title="'.$obj->artist().'" style="max-width:'.$obj->width().'px;"/>
        <figcaption class="clearfix">© '.date('Y').' '.$artist.'</figcaption>
    </figure>
    <div class="keywords">
        <strong>See also</strong>
        <ul class="clearfix">'.$data.'</ul>
        <p id="logged-in-image-link">http://www.nbillustration.co.uk/image/'.$id.'/</p>
    </div>';
    
} else if($action == 'IMAGE_DATA_SHORT'){
    if(isset($_REQUEST['id'])){ $id = $_REQUEST['id']; } else { echo 'ERROR'; exit; }
    $obj = new Image($id);
    echo '
    <figure>
        <img src="'.$obj->src().'" title="'.$obj->artist().'" style="max-width:'.$obj->width().'px;"/>
        <figcaption class="clearfix">© '.date('Y').' '.$obj->artist().'</figcaption>
    </figure>';
    
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.