Title: PHP Function getImage, Need to modify it
Last modified: August 19, 2016

---

# PHP Function getImage, Need to modify it

 *  [amamam](https://wordpress.org/support/users/amamam/)
 * (@amamam)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/php-function-getimage-need-to-modify-it/)
 * Hi all,
    I’ve got a theme with this function, that pick the first image of an
   article and put it in my index page:
 *     ```
       function getImage($num) {
       	global $more;
       	$more = 1;
       	$link = get_permalink();
       	$content = get_the_content();
       	$count = substr_count($content, '<img');
       	$start = 0;
       	for($i=1;$i<=$count;$i++) {
       		$imgBeg = strpos($content, '<img', $start);
       		$post = substr($content, $imgBeg);
       		$imgEnd = strpos($post, '>');
       		$postOutput = substr($post, 0, $imgEnd+1);
       		$result = preg_match('/width="([0-9]*)" height="([0-9]*)"/', $postOutput, $matches);
       		if ($result) {
       			$pagestring = $matches[0];
       			$image[$i] = str_replace($pagestring, "", $postOutput);
       		} else {
       			$image[$i] = $postOutput;
       		}
       		$start=$imgEnd+1;
       	}
       	if(stristr($image[$num],'<img')) { echo '<a href="'.$link.'">'.$image[$num]."</a>"; }
       	$more = 0;
       }
       ```
   
 * This is the output that this function returns:
 * `<a href="http://www.website.it/wp/?p=12"><img class="alignnone size-full wp-
   image-13" title="img_1" src="http://www.website.it/wp/wp-content/uploads/2009/
   10/img_1.jpg" alt="img_1" /></a>`
 * But, in the output html code I need to be specified the height and width of the
   image. How can I do this?
    I don’t understand PHP so much for now, but I need
   to do this for work, thanks for any help 🙂

The topic ‘PHP Function getImage, Need to modify it’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [amamam](https://wordpress.org/support/users/amamam/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/php-function-getimage-need-to-modify-it/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
