High CPU consumption by a PHP script
<p>My hosting service provider has disabled my account 2 times due to one of my image resizing PHP scripts. The script is <a href="http://plugins.svn.wordpress.org/amtythumb/trunk/lead-img.php" rel="nofollow">lead-img</a>, which is pasted here:</p>

<pre><code>include ("videothumb.php");

function amty_lead_img($w='',$h='',$constrain='',$img='',$percent='',$zc='',$post_id = '',
                        $img_url_only = 'y',$show_default = 'y',$default_img = '') {    
    if($img == ''){
        if($post_id == ''){
            global $id;
            $img = amty_take_first_img_by_id($id);
        }
        else
            $img = amty_take_first_img_by_id($post_id);
    }

    if($img =='' &amp;&amp; $show_default != 'y'){
        $img_url = $img;
    }else{
        if($img =='' &amp;&amp; $show_default == 'y'){
            if($default_img != '')
                $img = $default_img;
            else
                $img = WP_PLUGIN_URL . "/amtythumb/amtytextthumb.gif";
        }
        if($constrain != '')
            $constrain='constrain='. $constrain . '&amp;';
        if($h != '')
            $h='h='. $h . '&amp;';
        if($w != '')
            $w='w='. $w . '&amp;';
        if($zc != '')
            $zc='zc='. $zc . '&amp;';
        if($percent != '')
            $percent='percent='. $percent . '&amp;';
        $img_url = WP_PLUGIN_URL . "/amtythumb/scripts/imgsize.php?".$zc."". $percent."".$constrain."" . $w ."" . $h ."&amp;img=" . $img ;
    }

    if($img_url_only == "y"){
        $out = $img_url;
    }else{
        $out = '&lt;img src="'.$img_url.'" /&gt;';
    }
    //echo $out;
    return $out;
}//function end

function amty_take_first_img_by_id($id) {
    $temp = $wp_query;  // assign orginal query to temp variable for later use
    $wp_query = null;
        global $wpdb;
      $img='';
      $attach_img='';
      $uploaded_img = '';

      $image_data = $wpdb-&gt;get_results("SELECT guid, post_content, post_mime_type, post_title FROM wp_posts WHERE id = $id");
      $match_count = preg_match_all("/&lt;img[^']*?src=\"([^']*?)\"[^']*?\/?&gt;/", $image_data[0]-&gt;post_content, $match_array, PREG_PATTERN_ORDER);
      if($match_count == 0){

          /*$match_count = preg_match_all("/&lt;img[^']*?src=\"([^']*?)\"[^']*?&gt;/", $image_data[1]-&gt;post_content, $match_array, PREG_PATTERN_ORDER);
          if($match_count == 0){
              $match_count = preg_match_all("/&lt;img[^&gt;]+&gt;/i", $image_data[1]-&gt;post_content, $match_array, PREG_PATTERN_ORDER);
              if($match_count == 0)
                    $match_count = preg_match_all("/&lt;img[^']*?src=\"([^']*?)\"[^']*?\/&gt;/", $image_data[0]-&gt;post_content, $match_array, PREG_PATTERN_ORDER);
                    if($match_count == 0){*/
                      $img = thumb($image_data[0]-&gt;post_content);
                    /*}
        }*/
      }


      if( $img == '') $img = $match_array[1][0];

      $attach_img = amty_get_firstimage($output-&gt;guid);

      $first_image_data = array ($image_data[0]);
      foreach($first_image_data as $output) {
      if (substr($output-&gt;post_mime_type, 0, 5) == 'image'){
            $uploaded_img = $output-&gt;guid;
            break;
        }
      }

$wp_query = $temp;
if( $img != '') return $img;
if( $attach_img != '')  return $attach_img;
if( $uploaded_img != '')    return $uploaded_img;
return '';
}

//get First attached image
function amty_get_firstimage($post_id='', $size='thumbnail') {
     $id = (int) $post_id;
     $args = array(
      'post_type' =&gt; 'attachment',
      'post_mime_type' =&gt; 'image',
      'numberposts' =&gt; 1,
      'order' =&gt; 'ASC',
      'orderby' =&gt; 'menu_order ID',
      'post_status' =&gt; null,
      'post_parent' =&gt; $id
     );
     $attachments = get_posts($args);
     if ($attachments) {
       $img = wp_get_attachment_image_src($attachments[0]-&gt;ID, $size);
       return $img[0];
     }else{
       return '';
     }
}

?&gt;
</code></pre>

<p>This script is calling another script "videothumb.php" for accessing image for a video url from respective site and to resizing it.</p>

<p>I am not able to figure out anything wrong with the script. I checked the log to ensure how many times it was called etc. But everything seems normal.</p>

<p>Can someone guide me which part of script can slow down the CPU or can increase server load?</p>
