# 画像の表示を縮小(リサイズ)したいのですが

**URL:** <https://community.cybozu.dev/t/topic/909>\
**Category:** kintone 開発相談\
**Created:** [2017 年 9 月 15 日午前 2:20 UTC](https://community.cybozu.dev/t/topic/909 "2017-09-15T02:20:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account1890](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@Legacy\_Account1890](https://community.cybozu.dev/u/Legacy_Account1890)\
**Post date:** [2017 年 9 月 15 日午前 2:20 UTC](https://community.cybozu.dev/t/topic/909/1 "2017-09-15T02:20:08Z")

</div>

スペースのフィールドに画像を表示させたのですが、元の画像がスペースよりも大きいとスペースの枠にスクロールバーが表示されます。

表示の際にスペースの枠に自動的に合わせて画像を表示できるようにしたいのですが、教えてください。

&nbsp;現在設定しているjavascriptは以下となっております。

(function () {  
“use strict”;

function displayscratch5(event) {

var record = event[‘record’];

if (record[‘url5’][‘value’] === undefined){return;}  
if (record[‘url5’][‘value’].length == 0){return;}

var scratch5\_image\_Url = record[‘url5’][‘value’];  
scratch5\_image\_Url = scratch5\_image\_Url.replace(‘http://’,‘https://’);  
scratch5\_image\_Url = scratch5\_image\_Url.replace(‘watch?v=’,‘embed/’);

var elscratch5 = document.createElement(‘div’);  
elscratch5.setAttribute(‘id’, ‘dsp\_scratch5’);  
elscratch5.setAttribute(‘name’, ‘dsp\_scratch5’);

var elIframe = document.createElement(‘iframe’);  
elIframe.setAttribute(‘src’, scratch5\_image\_Url);  
elIframe.setAttribute(‘style’, ‘width: 550px; height: 555px’);  
elIframe.setAttribute(‘frameborder’, ‘0’);  
elscratch5.appendChild(elIframe);

var el = kintone.app.record.getSpaceElement(‘scratch5’);  
el.appendChild(elscratch5);

var elParent = el.parentNode;  
elParent.setAttribute(‘style’, ‘width: 550px; height: 555px’);  
}

kintone.events.on(‘app.record.detail.show’, displayscratch5);  
})();

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1740](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Legacy\_Account1740](https://community.cybozu.dev/u/Legacy_Account1740)\
**Post date:** [2017 年 9 月 15 日午前 11:02 UTC](https://community.cybozu.dev/t/topic/909/2 "2017-09-15T11:02:27Z")

</div>

ススム様

お世話になっております。  
cstapの江田です。

以下のようなコードで実装できるかと思います。

```
(function () {
"use strict";
function displayscratch5(event) {

var record = event['record'];

if (record['url5']['value'] === undefined){return;}
if (record['url5']['value'].length == 0){return;}

var scratch5_image_Url = record['url5']['value'];
scratch5_image_Url = scratch5_image_Url.replace('http://','https://');
scratch5_image_Url = scratch5_image_Url.replace('watch?v=','embed/');

var elscratch5 = document.createElement('div');
elscratch5.setAttribute('id', 'dsp_scratch5');
elscratch5.setAttribute('name', 'dsp_scratch5');

var img = new Image();
img.src = scratch5_image_Url;
img.onload = function(){
var imgWidth = img.width;
var imgHeight = img.height;
var width = 550; //表示したい幅(px)
var height = 555; //表示したい高さ(px)
var rWidth = width / imgWidth;
var rheight = height / imgHeight;
var r = rWidth < rheight ? rWidth : rheight;

var elIframe = document.createElement('iframe');
elIframe.setAttribute('src', scratch5_image_Url);
elIframe.setAttribute('style', 'width: ' + imgWidth + 'px; height: ' + imgHeight + 'px; -webkit-transform: scale(' + r + '); -webkit-transform-origin:0 0; transform: scale(' + r + '); transform-origin:0 0;');
elIframe.setAttribute('frameborder', '0');
elscratch5.appendChild(elIframe);

var el = kintone.app.record.getSpaceElement('scratch5');
el.appendChild(elscratch5);

var elParent = el.parentNode;
elParent.setAttribute('style', 'width: ' + width + 'px; height: ' + height + 'px; overflow: hidden;');
};

}
kintone.events.on('app.record.detail.show', displayscratch5);
})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1890](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@Legacy\_Account1890](https://community.cybozu.dev/u/Legacy_Account1890)\
**Post date:** [2017 年 9 月 19 日午前 1:41 UTC](https://community.cybozu.dev/t/topic/909/3 "2017-09-19T01:41:10Z")

</div>

**[江田](https://community.cybozu.dev/hc/ja/profiles/5826575706-%E6%B1%9F%E7%94%B0%E7%AF%A4%E5%8F%B2)さま**

&nbsp;

**無事、イメージ通りの表示ができました。**

**ご教示頂きまして、ありがとうございました。**
