• 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="https://www.website.it/wp/?p=12"><img class="alignnone size-full wp-image-13" title="img_1" src="https://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.