Chrisnaj Posted July 16, 2018 Share Posted July 16, 2018 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! Quote Link to comment Share on other sites More sharing options...
Barand Posted July 16, 2018 Share Posted July 16, 2018 (edited) 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 July 16, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
Chrisnaj Posted July 16, 2018 Author Share Posted July 16, 2018 Hi Barand, thanks for getting back so quickly. Just wondering if you've got an answer to my specific question re dividing the image width in 2 using the code I quoted? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 16, 2018 Share Posted July 16, 2018 Not from the code you posted - a php line directly followed by an html line. Can you post the relevant current "working" code so I can see it in context. In your Image class, are src(), artist() and width() really methods or are they properties? Quote Link to comment Share on other sites More sharing options...
Chrisnaj Posted July 16, 2018 Author Share Posted July 16, 2018 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 16, 2018 Share Posted July 16, 2018 (edited) try $halfwidth = $obj->width() / 2; echo '<img src="'.$obj->src().'" title="'.$obj->artist().'" style="max-width:'.$halfwidth.'px;"/>'; Edited July 16, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
Chrisnaj Posted July 16, 2018 Author Share Posted July 16, 2018 Thank you very much for your help ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.