• 解決済 haruca

    (@haruca)


    先日もこちらで質問させていただきました。
    度々失礼いたします。
    http://suprb.com/#gridalicious
    こちらのテーマの利用を考えています。
    phpについて無知な為、自力で色々調べてみたのですがどうしようもなく
    どなたかご教授いただけたらと思います。

    single.phpで表示した個別ページに
    画像がある場合、
    表示に問題は無いのですが、ソースの中に

    <b>Warning</b>: Division by zero in <b>/home/sites/***/.../themes/grid-a-licious/single.php</b> on line <b>96</b><br />

    というエラーが出てしまいます。

    該当箇所(96行目)を確かめると

    $imagesize = getimagesize($img_url);
    list($width, $height, $type, $attr) = getimagesize($img_url);
    $theClass = "eachpost ";
    if ( $width < "440" ) { $new_width = "200"; $theClass = "eachpost "; }
    else if ( $width < "660" ) { $new_width = "430"; $theClass = "eachpost twocols"; }
    else { $new_width = "660"; $theClass = "eachpost threecols"; }
    $new_height = ($height*$new_width)/$width;


    $new_height = ($height*$new_width)/$width;

    の部分のようなのですが、ゼロ除算のエラーだということは分かったのですが、実際画像の幅は0では無いので・・・どうすればいいのか分かりません。

    また、index.phpにも同じ記述があるのですが、こちらはエラーは出ません。
    記事中に画像が無い場合もエラーは出ません。
    表にエラー表示が出ていないので、このままでもいいのか?とも思うのですが、
    なんとも気がかりです。

    どなかた対処方が分かる方がいらしたら、
    ご教授お願いいたします。

12件の返信を表示中 - 1 - 12件目 (全12件中)
  • 画像ライブラリの問題じゃないでしょうか?
    入ってないとか、正しいものでない、とか

    もしくは画像そのものの問題とか

    サーバ環境はどんな感じでしょうか?

    トピック投稿者 haruca

    (@haruca)

    >kvexさま

    回答どうもありがとうございます。

    画像ライブラリというのは、php-GDというものでしょうか??
    (調べている際に少し出てきたのですが、あまりよく分かっていません・・・)

    サーバ環境は
    ・・・どのように書けばよいのか分からないのですが、
    ロリポップで
    PHP Version 5.2.6
    です。

    GD Support enabled
    GD Version bundled (2.0.34 compatible)

    とも書いてあります。

    よろしくお願いいたします><

    なるほど、ではどんな画像を登録してもそうなりますか?

    それから
    $imagesize = getimagesize($img_url);
    のときに有効なuriを渡してるかも気になりますね。
    ためしに
    var_dump($img_url);
    $imagesize = getimagesize($img_url);
    などとして正しいuriが渡されてるか確認とかできますかね?
    公開中のサイトだとアレですかね。

    トピック投稿者 haruca

    (@haruca)

    >kvexさま

    度々の回答、どうもありがとうございます。

    教えていただいたソースに書き換えてみたところ、
    表示に特に変わりはなく、
    見た目は、書き換え前と同じように画像もリサイズされて表示されているのですが、
    ソースの中身をを見ると

    string(46) “wp-content/uploads//2009/09/xxx.jpg”

    などのエラー?が出ていました。

    <img src=””>
    の中は正しい画像のパスが書いてありました。

    よろしくお願いいたします。

    トピック投稿者 haruca

    (@haruca)

    追記です

    今、自分で表示されたページのソースを見ていて気付いたのですが、

    写真が1枚でも2枚でも3枚でも、
    必ず
    <div class=”eachpost “>
    <div><img src=”” width=”200″ height=”” /></div></div>

    という、画像が空のソースが一緒に生成されていました。

    なので、safariやChromeで見たら画像割れ(?)のアイコンがおもてに出てしまっています。
    他の写真のサイズがいくつでも、必ず width=”200″で書かれていて中身は空・・・
    コレがゼロ除算の正体でしょうか??

    この空ソースが生成されないようにするにはどのようにすればいいのでしょうか??

    あれ・・・ちょっと待って下さいね

    設定→その他の設定

    「アップロードするファイルの保存場所」

    wp-content/uploads/
    となっていませんか?
    wp-content/uploads
    にしたらダメです?

    それと、そのエラーの箇所には関係ないですけど
    if ( $width < “440” ) { $new_width = “200”; $theClass = “eachpost “; }

    こういうのもどうかと思いますね

    if ( $width < 440 ) { $new_width = 200; $theClass = “eachpost “; }
    こうなってて欲しい気がします。
    その他の行も同様

    モデレーター jim912

    (@jim912)

    harucaさん、kvexさん、横やり失礼します。

    kvexさんにご指摘いただいた点もおかしいですが、$img_urlには、サーバ内のフルパスもしくは、httpから始まる絶対アドレスが入っていて欲しいところです。

    $img_urlに入る値がどのように作られているか知りたいので、single.phpの前後の部分も含めて提示してみていただけますか?

    ※ 私の環境が悪いのかわかりませんが、リンクされているテーマのダウンロードからだと、ファイル数が全く足りず、自環境での検証ができないのです。。。

    トピック投稿者 haruca

    (@haruca)

    >kvexさま

    回答、ありがとうございます。

    「アップロードするファイルの保存場所」は
    デフォルトから触っていなくて
    wp-content/uploads
    になっています。

    ただ、上に書いていただいた
    var_dump($img_url);
    $imagesize = getimagesize($img_url);
    の記述をいれたときに出てきた

    string(46) “wp-content/uploads//2009/09/xxx.jpg”

    のエラーのみ、uploads//2000…
    になっており、
    その下に生成された画像タグの中身は
    <img src=”http://www/・・・/wp-content/uploads/2009/09/xxx.jpg”&gt;
    と、正しく生成されています・・・・

    widthの” ”も、全部はずしてみたのですが、ダメでした・・・(;_;)

    トピック投稿者 haruca

    (@haruca)

    >jim912さま

    どうもありがとうございます。

    single.phpをそのままコピペします。
    デフォルトもあるのですが、ナビゲーション部分を少し自分でいじったので、
    そちらを下記にコピぺします。

    <?php get_header(); ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <div class="eachpost ">
    	<div class="bold">
    		<?php the_title(); ?><br />
    	</div>
    </div>
    
    	<?php
    		$content = get_the_content($more_link_text, $stripteaser, $more_file);
    		$content = apply_filters('the_content', $content);
    		$content = preg_replace('/<img[^>]+./','', $content);
    		$content = str_replace('<p>','',$content);
    		$content = str_replace('</p>','<br /><br />',$content);
    		$content = str_replace('<div class="eachpost "><div></div></div>','',$content);
    		$content = str_replace('<div class="eachpost twocols"><div></div></div>','',$content);
    		$content = str_replace('<div class="eachpost threecols"><div></div></div>','',$content);
    		$content = str_replace(']]>', ']]>', $content);
    	?>
    	<div class="eachpost twocols">
    		<div>
    			<?php echo $content; ?>
    		</div>
    	</div>
    	<div class="eachpost">
    		<div>
    		<?php the_time('M d, Y'); ?> <?php _e("at"); ?> <?php the_time('g:i a'); ?>.<br />
    		Filled under: <?php the_category(',') ?> <?php the_tags('Tags:', ', ', ''); ?>
    		</div>
    	</div>
    		<div class="eachpost">
    		<div>
    		<?php previous_post('%','&mdash;Older', 'no'); ?>
    		</div>
    	</div>
    		<div class="eachpost">
    		<div>
    		<?php next_post('%','Newer&mdash;', 'no'); ?>
    		</div>
    	</div>
    
    <div class="main">
    
    	<?php
    		$imgcontent = get_the_content($more_link_text, $stripteaser, $more_file);
    		$imgcontent = apply_filters('the_content', $imgcontent);
    		$imgcontent = preg_replace('/<p[^>]+./','', $imgcontent);
    		$imgcontent = str_replace('<p>','',$imgcontent);
    		$imgcontent = str_replace('</p>','',$imgcontent);
    
    		if ( strstr($imgcontent,'<div class="eachpost')) {
    			echo $imgcontent;
    		} else {
    
    			// gallery not found;
    
    			$images = explode("<img ", $imgcontent);
    			if (count($images) != "1") {
    			for ($i=0;$i<count($images);$i++) {
    
    				if ($images[$i] != "") {
    					$image = str_replace('<img ','<div class="eachpost "><div><img ','<img '.$images[$i]);
    					$imagepieces = explode("/>", $image);
    					$imageurl = substr($imagepieces[0], 0, -2) . '" /></div></div>';
    
    					$imagesrc_temp = explode('src="', $imageurl);
    					$imagesrc = explode('"', $imagesrc_temp[1]); // get src
    
    					$imagew_temp = explode('width="', $imageurl);
    					$imagew = explode('"', $imagew_temp[1]); // get width
    
    					$imageh_temp = explode('height="', $imageurl);
    					$imageh = explode('"', $imageh_temp[1]); // get height
    
    					$upload_path = get_option( 'upload_path' );
    					$upload_path = trim($upload_path);
    					$img_full_url = $imagesrc[0];
    
    					$temp = substr(strrchr($upload_path, "/"), 1);
    
    					list($url, $filename) = explode($temp, $img_full_url);
    
    					$img_url = $upload_path . "/" . $filename;
    
    					$imagesize = getimagesize($img_url);
    					list($width, $height, $type, $attr) = getimagesize($img_url);
    					$theClass = "eachpost ";
    					if ( $width < "440" ) { $new_width = "200"; $theClass = "eachpost "; }
    						else if ( $width < "660" ) { $new_width = "430"; $theClass = "eachpost twocols"; }
    							else { $new_width = "660"; $theClass = "eachpost threecols"; }
    
    					$new_height = ($height*$new_width)/$width;
    
    					echo '<div class="'.$theClass.'">
    					<div>';
    
    					echo '<img src="' . $img_full_url . '" width="' . $new_width . '" height="' . $new_height . '" /></div></div>'; }
    
    				}
    			}
    		}
    		?>
    
    </div>
    
    <div class="eachpost twocols">
    	<div>
       		<?php if ( comments_open() ) comments_template(); ?>
    	</div>
    </div>   
    
    <?php endwhile; else: ?>
    <div class="warning">
    	<p>Sorry, but you are looking for something that isn't here.</p>
    </div>
    <?php endif; ?>
    
        <?php get_footer(); ?>

    です。

    よろしくお願いします。

    遅い時間に申し訳ありません・・・・

    モデレーター jim912

    (@jim912)

    harucaさん、ソースありがとうございます。

    確認と検証をお願いいたします。

    写真が1枚でも2枚でも3枚でも、
    必ず
    <div class=”eachpost “>
    <div><img src=”” width=”200″ height=”” /></div></div>

    という、画像が空のソースが一緒に生成されていました。

    この現象は、表示される画像の前に必ず1回だけ出現していますか?

    そして、

    for ($i=0;$i<count($images);$i++) {

    for ($i=1;$i<count($images);$i++) {

    に直した場合、上記の現象とWarningエラーが止まりますか?

    また上記の変更に伴い、画像が投稿欄の一番最初にある場合やテキストがあってその後に画像が入る場合などで画像が消えたりしないかも確認してみてください。

    jim312 さんのfor文の修正だけでいいっぽいですね。
    いま、試しにやってみました。
    それから、どうしても条件分岐でクオートされてるのが気持ち悪いので修正した内容です。

    <?php get_header(); ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <div class="eachpost ">
    	<div class="bold">
    		<?php the_title(); ?><br />
    	</div>
    </div>
    
    	<?php
    		$content = get_the_content($more_link_text, $stripteaser, $more_file);
    		$content = apply_filters('the_content', $content);
    		$content = preg_replace('/<img[^>]+./','', $content);
    		$content = str_replace('<p>','',$content);
    		$content = str_replace('</p>','<br /><br />',$content);
    		$content = str_replace('<div class="eachpost "><div></div></div>','',$content);
    		$content = str_replace('<div class="eachpost twocols"><div></div></div>','',$content);
    		$content = str_replace('<div class="eachpost threecols"><div></div></div>','',$content);
    		$content = str_replace(']]>', ']]>', $content);
    	?>
    	<div class="eachpost twocols">
    		<div>
    			<?php echo $content; ?>
    		</div>
    	</div>
    	<div class="eachpost">
    		<div>
    		<?php the_time('M d, Y'); ?> <?php _e("at"); ?> <?php the_time('g:i a'); ?>.<br />
    		Filled under: <?php the_category(',') ?> <?php the_tags('Tags:', ', ', ''); ?>
    		</div>
    	</div>
    		<div class="eachpost">
    		<div>
    		<?php previous_post('%','&mdash;Older', 'no'); ?>
    		</div>
    	</div>
    		<div class="eachpost">
    		<div>
    		<?php next_post('%','Newer&mdash;', 'no'); ?>
    		</div>
    	</div>
    
    <div class="main">
    
    	<?php
    		$imgcontent = get_the_content($more_link_text, $stripteaser, $more_file);
    		$imgcontent = apply_filters('the_content', $imgcontent);
    		$imgcontent = preg_replace('/<p[^>]+./','', $imgcontent);
    		$imgcontent = str_replace('<p>','',$imgcontent);
    		$imgcontent = str_replace('</p>','',$imgcontent);
    
    		if ( strstr($imgcontent,'<div class="eachpost')) {
    			echo $imgcontent;
    		} else {
    
    			// gallery not found;
    
    			$images = explode("<img ", $imgcontent);
    			if (count($images) > 1) {
    			for ($i=1;$i<count($images);$i++) {
    
    				if ($images[$i] != "") {
    					$image = str_replace('<img ','<div class="eachpost "><div><img ','<img '.$images[$i]);
    					$imagepieces = explode("/>", $image);
    					$imageurl = substr($imagepieces[0], 0, -2) . '" /></div></div>';
    
    					$imagesrc_temp = explode('src="', $imageurl);
    					$imagesrc = explode('"', $imagesrc_temp[1]); // get src
    
    					$imagew_temp = explode('width="', $imageurl);
    					$imagew = explode('"', $imagew_temp[1]); // get width
    
    					$imageh_temp = explode('height="', $imageurl);
    					$imageh = explode('"', $imageh_temp[1]); // get height
    
    					$upload_path = get_option( 'upload_path' );
    					$upload_path = trim($upload_path);
    					$img_full_url = $imagesrc[0];
    
    					$temp = substr(strrchr($upload_path, "/"), 1);
    
    					list($url, $filename) = explode($temp, $img_full_url);
    
    					$img_url = $upload_path . "/" . $filename;
    					$imagesize = getimagesize($img_url);
    					list($width, $height, $type, $attr) = getimagesize($img_url);
    					$theClass = "eachpost ";
    					if ( $width < 440 ) { $new_width = 200; $theClass = "eachpost "; }
    						else if ( $width < 660 ) { $new_width = 430; $theClass = "eachpost twocols"; }
    							else { $new_width = 660; $theClass = "eachpost threecols"; }
    
    					$new_height = ($height*$new_width)/$width;
    
    					echo '<div class="'.$theClass.'">
    					<div>';
    
    					echo '<img src="' . $img_full_url . '" width="' . $new_width . '" height="' . $new_height . '" /></div></div>'; }
    
    				}
    			}
    		}
    		?>
    
    </div>
    
    <div class="eachpost twocols">
    	<div>
       		<?php if ( comments_open() ) comments_template(); ?>
    	</div>
    </div>   
    
    <?php endwhile; else: ?>
    <div class="warning">
    	<p>Sorry, but you are looking for something that isn't here.</p>
    </div>
    <?php endif; ?>
    
        <?php get_footer(); ?>
    トピック投稿者 haruca

    (@haruca)

    >jim912さま、kvexさま

    お二人に教えていただいたとおりに直したところ、
    無事、エラーがでなくなりました!!
    写真の枚数をふやしたり、挿入箇所を変えてみたり、
    色々検証しましたが、全部OKでした。

    本当にどうもありがとうございました^^

12件の返信を表示中 - 1 - 12件目 (全12件中)
  • トピック「Warning: Division by zero ・・・のエラー」には新たに返信することはできません。