mayaの実験場

この人生は沼だらけ

AWS Lightsailで独自ドメイン入れたらgetimagesizeでConnection refusedした話

f:id:xx_maya_xx:20191211132818p:plain

仕様要件

AWS Lightsail上のAmazon Linuxにて運用。
Lightsail側で独自ドメイン適用。
あとmod_rewrite.htaccessで強制SSL
画像群は同一サーバに置く。S3は使わない。

同一サーバ内の画像フォルダの画像をギャラリー表示したいオーダー。
今回は画像のギャラリーにPhotoSwipeを利用。

photoswipe.com

このリンクの「How to build an array of slides from a list of links」を適用したかったので

photoswipe.com

<div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">

    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
        <a href="large-image.jpg" itemprop="contentUrl" data-size="600x400">
            <img src="small-image.jpg" itemprop="thumbnail" alt="Image description" />
        </a>
        <figcaption itemprop="caption description">Image caption</figcaption>
    </figure>

    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
        <a href="large-image.jpg" itemprop="contentUrl" data-size="600x400">
            <img src="small-image.jpg" itemprop="thumbnail" alt="Image description" />
        </a>
        <figcaption itemprop="caption description">Image caption</figcaption>
    </figure>

</div>

こんな感じの記述が要求されていた。なに?画像の縦横のサイズをよこせとな?

ならばと、画像群を格納したarrayを回し

<?php foreach($photos as $key => $value):?>
<?php $picture_size = getimagesize((empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER['HTTP_HOST'].$value);?>
<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<div class="thumb photo_gallery_item" style="background-image:url(<?=$value?>)" href='<?=$value?>' data-size="<?=$picture_size[0]?>x<?=$picture_size[1]?>">
  <a href='<?=$value?>' itemprop="contentUrl" >&nbsp;</a>
  <img src="<?=$value?>" itemprop="thumbnail" alt="" style='display:none;' />
</div>
</figure>
<?php endforeach;?>

こうやってみた。

<?php $picture_size = getimagesize((empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER['HTTP_HOST'].$value);?>

この行で叱られました。orz

A PHP Error was encountered
Severity: Warning

Message: getimagesize(http://独自ドメイン/画像.jpg): failed to open stream: Connection refused

imgタグの中身のは出てくるんです。

<?php $picture_size = getimagesize("https://" . $_SERVER['HTTP_HOST'].$value);?>

強制SSLにしたのでこうしたが、改善されずorz

試しにサーバに入りwgetで画像のURLを叩くも、自分のサーバの443ポートが開いてない様なお叱りをいただく。

確かにawsのロードバランサの仕様はそれでいいんだが…

画像群は同一サーバに置く。S3は使わない。

そうでした。今回はこれでした。
同じサーバなんだからサーバ内の絶対パスにすれば解決じゃない。

<?php //$picture_size = getimagesize((empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER['HTTP_HOST'].$value);?>
<?php $picture_size = getimagesize( FCPATH .$value);?>

FCPATH は /var/www/html などのCIのインストールフォルダです。

これで、事なきを得ました。

さてもう年の瀬ですよっと。
週末はもう世田谷ボロ市、その次の週末は冬至いとこ煮食べたらあっという間にガキの使いです。
でもあまり追い詰められてる感はない、というか年々追い詰められてる感が薄れていく。何故だw