0

I have an image called 'progressbar.png'. Is there a way that I could crop this image, with PHP or Javascript, each time the page loads the image will crop depending on the progress percentage and be loaded into the page. For example, if the image was 200px wide, at 25% progress the image would be cropped to only 50px wide.

danieljmt
  • 37
  • 1
  • 6

3 Answers3

3

Make a div, set the background of the div to your image, and manipulate the div's width to whatever you want.

HTML:

<div id="progressbar"></div>

CSS:

#progressbar{
background:url(progressbar.png) top left;
height:progressbarheightpx;
}

Javascript:

document.getElementById('progressbar').style.width='50px'

Or it could be done in PHP by simply printing out the width directly to the div:

<div id="progressbar" width="<?php echo $percent_done*200;?>px"></div>
Sean Johnson
  • 5,567
  • 2
  • 17
  • 22
0

You could wrap the img tag in a div. Use css to mark the overflow as hidden and javascript to dynamically set the width. However you may want to look into jQueryUI's progress bar instead.

http://jqueryui.com/demos/progressbar/

amsprich
  • 201
  • 1
  • 8
0

Cropping serverside (with PHP) would require a request for a new file each time the percentage changes.

Cropping an image with JavaScript can be done by using <canvas> (see Copy and crop images in Javascript), although this would be a huge overhead.

Just use CSS, it has a couple of techniques to show only a cutout of an element. See CSS Display an Image Resized and Cropped, Clip images with HTML and CSS or How to show a region on a image with CSS

BTW: There is usually no need to use an image for a progress bar. It is much easier with a simple DOM element appropriately styled, HTML5 even offers the <progress> tag. See How to make a progress bar

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375